Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encourage to use == instead of === for writing unit tests. #36

Closed
HyukjinKwon opened this issue Jul 1, 2016 · 9 comments
Closed

Encourage to use == instead of === for writing unit tests. #36

HyukjinKwon opened this issue Jul 1, 2016 · 9 comments

Comments

@HyukjinKwon
Copy link
Member

FunSuite provides === for comparison. I see == and === are used in a mixed way across unit tests. It might be great if this is encouraged documented.

See http://www.scalatest.org/getting_started_with_fun_suite

ScalaTest lets you use Scala's assertion syntax, but defines a triple equals operator (===) to give you better error messages. The following code would give you an error indicating only that an assertion failed:

assert(1 == 2)
Using triple equals instead would give you the more informative error message, "1 did not equal 2":

assert(1 === 2)

@aarondav
Copy link
Contributor

aarondav commented Jul 1, 2016

As of I think Scala 2.10, ScalaTest's assert is a macro which does print nice error messages. If you're working in a 2.10+ codebase, I would recommend double equals, as triple equals can be hard to remember, especially when it's unnecessary.

@HyukjinKwon
Copy link
Member Author

HyukjinKwon commented Jul 2, 2016

@aarondav Thank you so much for your comment!
I feel like it would be great if it is documented regardless of which one is preferred because apparently they are being used in a mixed way in Spark and it seems some prefer == whereas other do === including Spark committers.

@HyukjinKwon
Copy link
Member Author

HyukjinKwon commented Jul 2, 2016

If it does not matter, then I would like to close this anyway.

@aarondav
Copy link
Contributor

aarondav commented Jul 3, 2016

Feel free to add a section recommending == -- I think having a guideline here is very reasonable, especially since there's some history behind it.

@HyukjinKwon HyukjinKwon changed the title Encourage to use === instead of == for writing unit tests. Encourage to use == instead of === for writing unit tests. Jul 4, 2016
@HyukjinKwon
Copy link
Member Author

HyukjinKwon commented Jul 4, 2016

Hi, @aarondav,
I just simply ran the tests as below (my scala version is 2.11.6):

  • assert(1 == 2)

    org.scalatest.exceptions.TestFailedException was thrown.
    
  • assert(1 === 2)

    1 did not equal 2
    

I ran this within my local and then also tried this with unit tests in Spark.

Am I missing something?

@HyukjinKwon
Copy link
Member Author

HyukjinKwon commented Jul 4, 2016

But it seems apparently okay with other comparisons for example as below (not using constant) although I don't know why:

val a = 1
val b = 2
assert(a == b)

produces

1 did not equal 2

So, If you think it is fine, I will submit a PR with saying things such as ...

"... == is encouraged because triple equals can be hard to remember, especially when it's unnecessary and this might confuse newbies of Scala developers ...".

@HyukjinKwon
Copy link
Member Author

Hm.. cc @rxin too. Do you have any preference or think it is something we should not bother for now?

@rxin
Copy link
Contributor

rxin commented Jun 30, 2017

I don't have a strong preference. It doesn't really hurt my eye ...

@HyukjinKwon
Copy link
Member Author

Let me close this for now. I will reopen when I happen to get a better reason.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants