Do you put unit tests in same project or another project?

后端 未结 15 1589
失恋的感觉
失恋的感觉 2020-12-02 05:23

Do you put unit tests in the same project for convenience or do you put them in a separate assembly?

If you put them in a separate assembly like we do, we end up wit

相关标签:
15条回答
  • 2020-12-02 06:14

    My unit tests always go in a separate project. In fact, for every project I have in my solution, there is a separate test project that goes along with it. Testing code is not application code and should not be intermingled with it. One advantage to keeping them in separate projects -- at least using TestDriven.Net -- is that I can right-click on a test project and run all the tests in that project, testing an entire library of application code with one click.

    0 讨论(0)
  • 2020-12-02 06:16

    I would say keep them separate.

    On top of the other reasons mentioned, having code and tests together skews test coverage numbers. When you report on unit test coverage - reported coverage is higher because the tests are covered when you run unit tests. When you report on integration test coverage, the reported coverage is lower because integration tests would not run unit tests.

    0 讨论(0)
  • 2020-12-02 06:17

    If NUnit framework is used, there is an additional reason to put the tests in the same project. Consider the following example of the production code mixed with unit tests:

    public static class Ext
    {
         [TestCase(1.1, Result = 1)]
         [TestCase(0.9, Result = 1)]
         public static int ToRoundedInt(this double d)
         {
             return (int) Math.Round(d);
         }
    }
    

    The unit tests here serve as documentation and specification to the code being tested. I do not know how to achieve this effect of self-documenting, with the tests located in a separate project. The user of the function would have to search for the tests to see those test cases, which is unlikely.

    Update: I know that such usage of TestCase attribute was not that the developers of NUnit intented, but why not?

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