Where do you put your unit test?

前端 未结 3 904
一个人的身影
一个人的身影 2020-12-10 01:11

I have found several conventions to housekeeping unit tests in a project and I\'m not sure which approach would be suitable for our next PHP project. I am trying to find the

相关标签:
3条回答
  • 2020-12-10 01:35

    I always go for #1. While it's nice that they are close together. My reasons are as followed:

    • I feel there's a difference between the core codebase and the unittests. I need a real separation.
    • End-users rarely need to look at unittests. They are just interested in the API. While it's nice that unittests provide a separate view on the code, in practice I feel it won't be used to understand it better. (more descriptive documentation + examples do).
    • Because end-users rarely need unittests, I don't want to confuse them with more files and/or methods.
    • My coding standards are not half as strict for unittests as they are for the core library. This is maybe just my opinion, but I don't care as much for coding standards in my tests.

    Hope this helps.

    0 讨论(0)
  • 2020-12-10 01:36

    The current best practice is to separate the unit tests into their own directory, #1. All of the "convention over configuration" systems do it like this, eg. Maven, Rails, etc.

    I think your alternatives are interesting and valid, and the tool support is certainly there to support them. But it's just not that popular (as far as I know). Some people object to having tests interspersed with production code. But it makes sense to me that if you always write unit tests, that they be located right with your code. It just seems simpler.

    0 讨论(0)
  • 2020-12-10 01:41

    I favour keeping unit-tests in separate source files in the same directory as production code (#3).

    Unit tests are not second-class citizens, their code must maintained and refactored just like production code. If you keep your unit tests in a separate directory, the next developer to change your production code may miss that there are unit tests for it and fail to maintain the tests.

    In C++, I tend to have three files per class:

    MyClass.h
    MyClass.cpp
    t_MyClass.cpp
    

    If you're using Vim, then my toggle_unit_tests plug-in for toggling between source and unit test files may prove useful.

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