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

后端 未结 8 1712
孤城傲影
孤城傲影 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:26

    I think it is just a convention now and as you said it is adopted by "every unit testing framework (I know of)". If you are using a framework it would be annoying to switch to another framework that uses the opposite convention. So (if you are writing a new unit testing framework for example) it would be preferable for you as well to follow the existing convention. I believe this comes from the way some developers prefer to write their equality tests:

    if (4 == myVar)
    

    To avoid any unwanted assignment, by mistake, writing one "=" instead of "==". In this case the compiler will catch this error and you will avoid a lot of troubles trying to fix a weird runtime bug.

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

    Surely it makes logical sense to put the expected value first, as it's the first known value.

    Think about it in the context of manual tests. A manual test will have the expected value written in, with the actual value recorded afterwards.

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