简体   繁体   English

我应该将单元测试放在一个单独的库中吗? 单独的子目录?

[英]Should i put unit tests in a separate library? Separate subdirectory?

I have a large library of code which has a few (much too few) unit tests; 我有一个庞大的代码库,它有一些(太少)单元测试; i am adding code to the library, trying to make unit tests for the new code (i also want to add unit tests for old code in future, when i get more experienced). 我正在向库中添加代码,尝试对新代码进行单元测试(我还希望将来为旧代码添加单元测试,当我获得更多经验时)。

We currently use a custom (not very elaborate) unit-testing framework; 我们目前使用自定义(非常精细)的单元测试框架; i would like to switch to boost.test . 我想切换到boost.test

My library layout is: 我的图书馆布局是:

  • mylib1 (300 files, 70000 lines of code) mylib1(300个文件,70000行代码)

    • src SRC
      • foo FOO
        • foo.cpp Foo.cpp中
        • foo_part.cpp foo_part.cpp
        • foo_part.h foo_part.h
        • UnitTests - should i move it outside mylib ? UnitTests - 我应该把它移到mylib外面吗?
          • foo_test1.cpp foo_test1.cpp
          • foo_test2.cpp foo_test2.cpp
          • foo_test3.cpp foo_test3.cpp
      • bar 酒吧
        • bar.cpp bar.cpp
        • bar_part.cpp bar_part.cpp
        • bar_part.h bar_part.h
        • UnitTests - does it belong here? UnitTests - 它属于这里吗?
          • bar_test1.cpp bar_test1.cpp
          • bar_test2.cpp bar_test2.cpp
          • bar_test3.cpp bar_test3.cpp
    • include 包括
      • foo.h foo.h中
      • bar.h bar.h
  • mylib2 (70 files, 7000 lines of code) mylib2(70个文件,7000行代码)

    • Looks like the mylib1 above 看起来像上面的mylib1
  • UnitTestFramework UnitTestFramework

    • src SRC
      • MockObject1.cpp - does it belong here? MockObject1.cpp - 它属于这里吗?
      • Asserter.cpp - boost.test will replace it Asserter.cpp - boost.test将取代它
      • TestFixture.cpp - boost.test will replace it TestFixture.cpp - boost.test将取代它
    • include 包括
      • MockObject1.h MockObject1.h
      • Asserter.h - boost.test will replace it Asserter.h - boost.test将取代它
      • TestFixture.h - boost.test will replace it TestFixture.h - boost.test将取代它

I am asking the following questions here: 我在这里问以下问题:

  • Should unit tests be part of mylib DLL? 单元测试应该是mylib DLL的一部分吗? (I link mylib1 and mylib2 into one DLL) (我将mylib1mylib2链接到一个DLL中)
  • Should source code for unit tests be located under the mylib1 directory? 单元测试的源代码应该位于mylib1目录下吗?
  • Since i have mylib1 and mylib2 , how should the corresponding unit-tests be located - in sibling directories, together, etc? 由于我有mylib1mylib2 ,如何定位相应的单元测试 - 兄弟目录,一起等?
  • Where should i put source code for mock objects? 我应该在哪里放置模拟对象的源代码? (they serve both mylib1 and mylib2 ) (它们同时服务于mylib1mylib2
  1. unit tests are kind of documentation, therefore I would leave the directory structure as it is 单元测试是一种文档,因此我会保留目录结构
  2. mocks/fakes are helpers in unit tests, therefore I wouldn't leave them in the source directory. mocks / fakes是单元测试中的助手,因此我不会将它们留在源目录中。 Create a new directory in the source, and move them there. 在源中创建一个新目录,并将其移动到那里。

Actually, I would simplify it even further : 实际上,我会进一步简化它:

 + mylib1 (300 files, 70000 lines of code)
    + unit_tests
      + test_foo.cpp
      + test_bar.cpp
    + mocks
      + mock_foo.hpp
      + mock_bar.hpp
    + foo.cpp
    + foo.hpp
    + bar.cpp
    + bar.hpp
 + mylib2
similar as for mylib1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM