运行在不同的进程加速单元测试 [英] Running Boost unit tests on different processes

查看:131
本文介绍了运行在不同的进程加速单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做单元测试在SystemC的程序。这个想法是有每间套房内多次测试多个测试套件。测试中的每一个都需要重置的SystemC架构(例如,通过调用 sc_simcontext ::复位()),但实际上是不可能的,由于一些错误,显然是不打算很快修复。所以,我决定拿出一个解决办法。

I want to do unit testing in a SystemC program. 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 (e.g., 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. Therefore, I decided to come up with a workaround.

我发现,如果我在不同的进程中运行每个测试一切正常。下面code片段给我用,使其工作方案概述:

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.

我一直在研究加速单元测试库的内部,我发现, unit_test_main 似乎是触发所有的测试执行的功能。但我不能想出一种非侵入性的方式,以在不同的进程中运行的各项测试加速单元测试来进行互动。

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. 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.

有谁知道在不同的进程中运行每个测试一个简单的解决方案吗?

Does anyone know of a simple solution for running each test on a different process?

推荐答案

我不是100%满意与我想出了解决方案,但我无论如何都会张贴。为方便起见,我封装的一切都变成一个命名空间:

I am not 100% satisfied with the solution that I came up, but I will post it anyway. For convenience, I encapsulated everything into a namespace:

头文件:

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()

源文件:

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

用法示例:

BOOST_AUTO_TEST_SUITE(suite)

SYSTEMC_TEST_CASE(test_case1) {
  // ...
}

SYSTEMC_TEST_CASE(test_case2) {
  // ...
}

BOOST_AUTO_TEST_SUITE_END()

的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);
}

每个测试用例现在在不同的进程中执行。因此,SystemC的没有任何问题单次执行期间运行多次。

Each test case will now be executed on a different process. Therefore, SystemC runs multiple times during a single execution without any problem.

该解决方案的唯一真正的问题是,由于某种原因,它是不可能的输出XML结果时要使用的文件下沉。但我发现,如果接收器是一切工作正常标准错误和输出重定向到一个文件中。

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. But I have found that everything works fine if the sink is stderr and the output is redirected to a file.

这篇关于运行在不同的进程加速单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆