如何通过boost::测试库来组织测试用例?

时间:2021-01-02 01:20:43

I have a project of 50+ .H/.CPP files/classes. I would like to test every class with its own test case, which will include methods for testing of different aspects of every class. My classes are located in different directories, like this:

我有一个50+。h /的项目。CPP文件/类。我想用它自己的测试用例来测试每个类,它将包括测试每个类不同方面的方法。我的类位于不同的目录中,像这样:

/project
  /include
    /SuperModule
      Foo.h
      Foo.cpp
      ..
    Alpha.h
    Alpha.cpp
    ..
  /test         // I assume that my tests shall be here
  main.cpp
  Makefile

I would like to use boost::test as a unit-testing framework. How should I organize my files, how shall I name them, etc. Some hint or a link or a suggestion will be appreciated. Thanks.

我想使用boost:::test作为单元测试框架。我应该如何组织我的文件,我应该如何命名它们,等等。一些提示或链接或建议将会被感激。谢谢。

1 个解决方案

#1


2  

We are using boost::test in a similar layout. Our layout is -

我们正在使用boost:::在类似的布局中进行测试。我们的布局,

/project
  /include
    /SuperModule
       /Foo
        foo.c
        foo.h
        /foo_unittest
            foo_unittest.c   // note - no separate header file is required 
                             // for boost::test unit test.exe program.

Basic layout rule is to put the unit test for a class in a sub-directory named "foo_unittest" after the class in the same directory as the source code. The advantage to this naming is

基本的布局规则是将类的单元测试放在一个名为“foo_unittest”的子目录中,以类的名字命名。这种命名的好处是

  1. The source code and the directory are stored next to each other. So by simple inspection you can see if you have written the unit test or not.
  2. 源代码和目录都存储在一起。通过简单的检查,你可以看到你是否写了单元测试。
  3. Also, when you copy the source code, it is easy to copy the unit test at the same time.
  4. 此外,在复制源代码时,很容易同时复制单元测试。

As our projects are not overly complex (30-50 major classes), this system works for us. If you are running larger projects, I don't think this would be an optimal solution.

由于我们的项目并不过于复杂(30-50个主要类),所以这个系统适合我们。如果您正在运行较大的项目,我认为这不是最佳的解决方案。

#1


2  

We are using boost::test in a similar layout. Our layout is -

我们正在使用boost:::在类似的布局中进行测试。我们的布局,

/project
  /include
    /SuperModule
       /Foo
        foo.c
        foo.h
        /foo_unittest
            foo_unittest.c   // note - no separate header file is required 
                             // for boost::test unit test.exe program.

Basic layout rule is to put the unit test for a class in a sub-directory named "foo_unittest" after the class in the same directory as the source code. The advantage to this naming is

基本的布局规则是将类的单元测试放在一个名为“foo_unittest”的子目录中,以类的名字命名。这种命名的好处是

  1. The source code and the directory are stored next to each other. So by simple inspection you can see if you have written the unit test or not.
  2. 源代码和目录都存储在一起。通过简单的检查,你可以看到你是否写了单元测试。
  3. Also, when you copy the source code, it is easy to copy the unit test at the same time.
  4. 此外,在复制源代码时,很容易同时复制单元测试。

As our projects are not overly complex (30-50 major classes), this system works for us. If you are running larger projects, I don't think this would be an optimal solution.

由于我们的项目并不过于复杂(30-50个主要类),所以这个系统适合我们。如果您正在运行较大的项目,我认为这不是最佳的解决方案。