Unit testing: why is the expected argument always first in equality tests?

后端 未结 8 1711
孤城傲影
孤城傲影 2020-12-15 15:34

Why is it that every unit testing framework (that I know of) requires the expected value in equality tests to always be the first argument:

Assert.AreEqual(42         


        
相关标签:
8条回答
  • 2020-12-15 16:08

    It seems that most early frameworks used expected before actual (for some unknown reason though, dice roll perhaps?). Yet with programming languages development, and increased fluency of the code, that order got reversed. Most fluent interfaces usually try to mimic natural language and unit testing frameworks are no different.

    In the assertion, we want to assure that some object matches some conditions. This is the natural language form, as if you were to explain your test code you'd probably say

    "In this test, I make sure that computed value is equal to 5"

    instead of

    "In this test, I make sure that 5 is equal to computed value".

    Difference may not be huge, but let's push it further. Consider this:

    Assert.That(Roses, Are(Red));
    

    Sounds about right. Now:

    Assert.That(Red, Are(Roses));
    

    Hm..? You probably wouldn't be too surprised if somebody told you that roses are red. Other way around, red are roses, raises suspicious questions. Yoda, anybody?

    That doesn't sound natural at all

    Yoda's making an important point - reversed order forces you to think.

    It gets even more unnatural when your assertions are more complex:

    Assert.That(Forest, Has.MoreThan(15, Trees));
    

    How would you reverse that one? More than 15 trees are being had by forest?

    This claim (fluency as a driving factor for modification) is somehow reflected in the change that NUnit has gone through - originally (Assert.AreEqual) it used expected before actual (old style). Fluent extensions (or to use NUnit's terminology, constraint based - Assert.That) reversed that order.

    0 讨论(0)
  • 2020-12-15 16:08

    Nobody knows and it is the source of never ending confusions. However not all frameworks follow this pattern (to a greater confusion):

    1. FEST-Assert uses normal order:

      assertThat(Util.GetAnswerToLifeTheUniverseAndEverything()).isEqualTo(42);
      
    2. Hamcrest:

      assertThat(Util.GetAnswerToLifeTheUniverseAndEverything(), equalTo(42))
      
    3. ScalaTest doesn't really make a distinction:

      Util.GetAnswerToLifeTheUniverseAndEverything() should equal (42)
      
    0 讨论(0)
  • 2020-12-15 16:08

    I think it's because JUnit was the precursor of most unit testing frameworks (not that it was the first unit testing framework, but it kicked off an explosion in unit testing). Since JUnit did it that way, all the subsequent frameworks copied this form and it became a convention.

    why did JUnit do it that way? I don't know, ask Kent Beck!

    0 讨论(0)
  • 2020-12-15 16:14

    My view for this would be to avoid any exceptions eg: 42.equals(null) vs null.equals(42)

    where 42 is expected null is actual

    0 讨论(0)
  • 2020-12-15 16:17

    Well they had to pick one convention. If you want to reverse it try the Hamcrest matchers. They are meant to help increase readability. Here is a basic sample:

    import org.junit.Test;
    import static org.junit.Assert.assertThat;
    import static org.hamcrest.core.Is.is;
    
    public HamcrestTest{
       @Test
       public void matcherShouldWork(){
           assertThat(   Math.pow( 2, 3 ),  is( 8 )  );
       }
    }
    
    0 讨论(0)
  • 2020-12-15 16:22

    I don't know but I've been part of several animated discussions about the order of arguments to equality tests in general.

    There are a lot of people who think

    if (42 == answer) {
      doSomething();
    }
    

    is preferable to

    if (answer == 42) {
      doSomething();
    }
    

    in C-based languages. The reason for this is that if you accidentally put a single equals sign:

    if (42 = answer) {
      doSomething();
    }
    

    will give you a compiler error, but

    if (answer = 42) {
      doSomething();
    }
    

    might not, and would definitely introduce a bug that might be hard to track down. So who knows, maybe the person/people who set up the unit testing framework were used to thinking of equality tests in this way -- or they were copying other unit testing frameworks that were already set up this way.

    0 讨论(0)
提交回复
热议问题