使用 Catch 包含测试用例的组合爆炸

Containing the combinatorial explosion of test cases using Catch

假设我有一个简单的 class,它在其构造函数中接受 6 个布尔参数,并根据这些参数的状态执行一些计算。

如果我想使用 Catch 充分测试所有情况,那么我需要 64 个单独的单元测试。

现在,假设在未来的某个时间点,我添加了第 7 个布尔参数。现在我要写的测试用例数量翻了一番,达到128个。

有没有一种方法可以设计我的单元测试,例如从单个测试规范自动 "generate" 所有 2^n 个测试用例?

您可以使用 Catch 的 generators 部分自动遍历您的函数采用的所有不同的 bool 组合:

TEST_CASE("where is my sandwich", "[hunger][food]")
{
  bool wantLettuce = GENERATE(Values(false, true));
  bool wantTomato = GENERATE(Values(false, true));
  bool wantBacon = GENERATE(Values(false, true));
  bool wantCheese = GENERATE(Values(false, true));
  bool wantEgg = GENERATE(Values(false, true));

  CHECK(sandwichAssembler(wantLettuce, wantTomato, wantBacon, wantCheese, wantEgg));
 }

IIRC 这将导致 Catch 运行 2^5 次尝试所有组合。

...但我假设您想要做的就是练习所有组合。如果您真的想验证输出在每种情况下都满足某些期望,则需要做更多的工作。

(我没有测试过这个 - 在巴塞罗那的一家咖啡馆等我的三明治时凭记忆回忆)