我可以在 CxxTest 中编写自定义断言吗?

发布于 2024-08-12 18:05:25 字数 1040 浏览 5 评论 0原文

我刚刚开始使用 CxxTest 并想测试 std::vector 是否已正确排序。这是我到目前为止的测试:

void testSort() {
  std::sort(vec.begin(), vec.end()); // This could be any sorting function

  for (unsigned int i = 0; i < vec.size() - 1; ++i) {
    TS_ASSERT(vec[i] <= vec[i + 1]);
  }
}

显然,CxxTest 不提供 TS_ASSERT_SORTED 断言,但是有没有办法编写自定义断言?这将使我能够做到这一点:

void testSort() {
  std::sort(vec.begin(), vec.end()); // This could be any sorting function

  TS_ASSERT_SORTED(vec);
}

以这种方式编写测试时,更容易看出测试的意图。

我浏览了 CxxTest 用户指南 但不知道是否可以编写自定义断言,例如这。作为替代方案,我可以编写一个 IsSorted 类并实现其 operator()。然后我可以这样编写测试:

void testSort() {
  std::sort(vec.begin(), vec.end()); // This could be any sorting function

  TS_ASSERT_PREDICATE(IsSorted, vec);
}

我猜这是正确的方法。但是,如果我这样做,我是否应该将 class IsSorted 的定义放在它自己的头文件中,与我的测试套件分开?我仍在尝试找出与单元测试相关的最佳实践,尤其是在这个框架中。

最后一个问题:我应该在 setUp() 方法中还是在测试本身中对向量进行排序?

I'm just starting to use CxxTest and would like to test whether a std::vector has been sorted correctly. Here's my test so far:

void testSort() {
  std::sort(vec.begin(), vec.end()); // This could be any sorting function

  for (unsigned int i = 0; i < vec.size() - 1; ++i) {
    TS_ASSERT(vec[i] <= vec[i + 1]);
  }
}

Obviously, CxxTest does not provide a TS_ASSERT_SORTED assertion, but is there a way to write custom assertions? That would allow me to do this:

void testSort() {
  std::sort(vec.begin(), vec.end()); // This could be any sorting function

  TS_ASSERT_SORTED(vec);
}

It's significantly easier to see the intent of the test when it's written this way.

I looked through the CxxTest user's guide but couldn't figure out whether you can write custom assertions like this. As an alternative, I could write a class IsSorted and implement its operator(). I could then write the test like this:

void testSort() {
  std::sort(vec.begin(), vec.end()); // This could be any sorting function

  TS_ASSERT_PREDICATE(IsSorted, vec);
}

I'm guessing this is the correct approach. If I do this, though, should I place the definition of class IsSorted in its own header file, separate from my test suite? I'm still trying to figure out the best practices associated with unit testing, especially in this framework.

One final question: should I be sorting the vector in the setUp() method or in the test itself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

混吃等死 2024-08-19 18:05:25

您可以通过更改 cxxtest 头文件来添加一些您自己的新宏。毕竟它是开源的!确保您通过这些宏正确地失败了测试(参见 TS_ASSERT_EQUALS 的作用,与 martiert 此处给出的示例不同,该示例与 cxxtest 的工作方式不兼容)。

You can add a few new macros of your own by changing cxxtest header files. It's open source, after-all! Make sure you fail the test correctly from those macros (see what TS_ASSERT_EQUALS do, unlike the example given here by martiert which is not compatible with how cxxtest work).

清浅ˋ旧时光 2024-08-19 18:05:25

好吧,断言只是预处理器宏,因此您可以根据需要实现它们,然后为它们 #define 一个宏。我认为最好使用不以 TS_ASSERT_ 开头的名称,这样就不会混淆后来的代码读者。

谓词版本对我来说看起来并不那么笨拙 - 我可能会这样做,或者可能在您正在测试的类上实现 check_ 方法,并从测试中调用它。我经常最终需要验证类的内部结构以进行运行时调试和验证的方法。无论如何,记录...

Well, the assertions are just preprocessor macros, so you could probably implement them however you want, and then #define a macro for them. I'd think it'd be better to use a name that doesn't start with TS_ASSERT_, so that you don't confuse later readers of the code.

The predicate version doesn't look all that clumsy to me - I'd probably go with that, or possibly implement a check_ method on the class you're testing, and call that from the test. I often end up needing methods that validate the internal structure of the class for run-time debugging & logging, anyway...

鯉魚旗 2024-08-19 18:05:25

你可以使用#define


#define TS_ASSERT_PREDICATE( vec )\
    std::vector tmp = vec;\
    for(int i = 0; i  tmp[i+1]) \
            return false;\ 
    }\ 
    return true;\ 
}

You could use a #define


#define TS_ASSERT_PREDICATE( vec )\
    std::vector tmp = vec;\
    for(int i = 0; i  tmp[i+1]) \
            return false;\ 
    }\ 
    return true;\ 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文