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

后端 未结 8 1735
孤城傲影
孤城傲影 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.

提交回复
热议问题