简体   繁体   English

运行Boost单元测试不同的进程

[英]Running Boost unit tests on different processes

I want to do unit testing in a SystemC program. 我想在SystemC程序中进行单元测试。 The idea is to have multiple test suites with several tests in each suite. 我们的想法是在每个套件中安装多个测试套件并进行多次测试。 Each one of the tests would require resetting the SystemC framework (eg, by calling sc_simcontext::reset() ), but that is actually not possible due to some bug that is apparently not going to be fixed anytime soon. 每个测试都需要重置SystemC框架(例如,通过调用sc_simcontext::reset() ),但实际上这是不可能的,因为某些错误显然不会很快修复。 Therefore, I decided to come up with a workaround. 因此,我决定提出一个解决方法。

I found out that if I run each test on a different process everything works fine. 我发现如果我在不同的进程上运行每个测试,一切正常。 The following code snippet gives an overview of the scheme I used to make it work: 以下代码段概述了我用于使其工作的方案:

void test1() {
  // ...
  sc_start();
}

void test2() {
  // ...
  sc_start();
}

typedef std::function<void()> TestFunction;

void run_test(TestFunction test_function) {
  pid_t pid = fork();
  switch (pid) {
  case -1:
    throw std::runtime_error("Error forking process");
  case 0:
    test_function();
    exit(0);
  default:
    waitpid(pid, nullptr, 0);
    break;
  }
}

int main() {
  run_test(test1);
  run_test(test2);
}

Now I want to implement such a testing scheme with Boost Unit Test. 现在我想用Boost单元测试来实现这样的测试方案。

I have been studying the internals of Boost Unit Test library and I have found that unit_test_main seems to be the function that triggers the execution of all the tests. 我一直在研究Boost单元测试库的内部结构,我发现unit_test_main似乎是触发所有测试执行的函数。 But I could not devise a non-intrusive way to interact with Boost Unit Test in order to run each test on a different process. 但我无法设计一种非侵入性的方式与Boost单元测试进行交互,以便在不同的进程上运行每个测试。

Does anyone know of a simple solution for running each test on a different process? 有谁知道在不同的进程上运行每个测试的简单解决方案?

I am not 100% satisfied with the solution that I came up, but I will post it anyway. 我对我提出的解决方案并不是100%满意,但无论如何我都会发布它。 For convenience, I encapsulated everything into a namespace: 为方便起见,我将所有内容封装到命名空间中:

Header file: 头文件:

namespace util {

typedef std::function<void()> TestFunction;

void run_test(TestFunction test_function);

} // namespace util

#define SYSTEMC_TEST_CASE(name)       \
  void name##_impl();                 \
  BOOST_AUTO_TEST_CASE(name) {        \
    util::run_test(name##_impl);      \
  }                                   \
  void name##_impl()

Source file: 源文件:

namespace util {

void run_test(TestFunction test_function) {
  pid_t pid = fork();
  switch (pid) {
    case -1:
      throw std::runtime_error("Error forking process");
    case 0:
      try { test_function(); }
      catch (const std::exception& e) {
        std::cout << boost::format("Exception caught: %1%") % e.what() << std::endl;
        exit(1);
      }
      catch (...) { exit(1); }
      exit(0);
    default:
      waitpid(pid, nullptr, 0);
      break;
  }
}

} // namespace util

Usage example: 用法示例:

BOOST_AUTO_TEST_SUITE(suite)

SYSTEMC_TEST_CASE(test_case1) {
  // ...
}

SYSTEMC_TEST_CASE(test_case2) {
  // ...
}

BOOST_AUTO_TEST_SUITE_END()

main.cpp contains: main.cpp包含:

#define BOOST_TEST_MODULE TestModule
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_NO_MAIN
#include <boost/test/unit_test.hpp>

#include <systemc.h>

boost::unit_test::test_suite* init_unit_test_suite(int, char*[]) {
  using namespace ::boost::unit_test;
  assign_op(framework::master_test_suite().p_name.value,
      BOOST_TEST_STRINGIZE(BOOST_TEST_MODULE).trim("\""), 0);
  return 0;
}

int sc_main(int argc, char* argv[]) {
  return boost::unit_test::unit_test_main(&init_unit_test, argc, argv);
}

Each test case will now be executed on a different process. 现在,每个测试用例将在不同的进程上执行。 Therefore, SystemC runs multiple times during a single execution without any problem. 因此,SystemC在单次执行期间运行多次而没有任何问题。

The only real issue of this solution is that for some reason it is not possible to use a file sink when outputting XML results. 此解决方案唯一真正的问题是,由于某种原因,在输出XML结果时无法使用文件接收器。 But I have found that everything works fine if the sink is stderr and the output is redirected to a file. 但我发现如果接收器是stderr并且输出被重定向到文件,一切正常。

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

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