抓住测试用例顺序 [英] Catch test case order
问题描述
我可以使用 保证多个 TEST_CASE
nofollow> Catch ?我使用LLVM测试一些代码,他们有一些可见的全局状态,我需要显式初始化。
现在我有一个测试用例,像这样: p>
TEST_CASE(,){
//初始化真正可靠的LLVM全局变量。
llvm :: InitializeAllTargets();
llvm :: InitializeAllTargetMCs();
llvm :: InitializeAllAsmPrinters();
llvm :: InitializeNativeTarget();
llvm :: InitializeAllAsmParsers();
//一些每个测试的设置我可以做成自己的函数
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile ...));
CHECK_NOTHROW(Interpret(...));
CHECK_THROWS(Compile(...));
CHECK_THROWS(Compile(...));
}
我想要的是将它重构为三个 TEST_CASE
,
- 一个用于应通过编译的测试,
- 一对于应该失败的测试,以及
- 一个用于应该通过解释的测试(以及将来,进一步的这种划分)。
但我不能简单地将测试内容移动到另一个 TEST_CASE
如果在设置不便的全局变量之前调用 TEST_CASE
,那么它们不会被初始化,测试将会失败。
我有点迟了,因为我只是看到它 - 对不起(以后你可以发布Catch相关问题到抓住论坛或 GitHub 。
无论如何,我不知道你在最后做了什么,但在这种情况下听起来像你只是想将每组断言分组到 SECTION
中。
TEST_CASE(){
//初始化真正的shitty LLVM全局变量
llvm :: InitializeAllTargets();
llvm :: InitializeAllTargetMCs();
llvm :: InitializeAllAsmPrinters );
llvm :: InitializeNativeTarget();
llvm :: InitializeAllAsmParsers();
SECTION(应该传递编译){
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile ...));
}
SECTION(should pass interpretation){
CHECK_NOTHROW(Interpret(...));
}
SECTION(应该失败编译){
CHECK_THROWS(Compile(...));
CHECK_THROWS(Compile(...));
}
}
每个部分都像嵌入式测试用例整个测试用例从开始到所有初始化 - 对每个部分执行)。因此,如果其中一个无异常抛出它不会阻止其他部分执行。
...除非初始化代码只执行一次 - 在这种情况下你可以放在一个静态初始化器,如@paddy建议(一个类,在其构造函数中调用初始化器 - 然后只是创建一个全局实例),或者你可以保护初始化代码块与一个静态bool。 / p>
Can I guarantee the order of execution with multiple TEST_CASE
s with Catch? I am testing some code using LLVM, and they have some despicable global state that I need to explicitly initialize.
Right now I have one test case that's like this:
TEST_CASE("", "") {
// Initialize really shitty LLVM global variables.
llvm::InitializeAllTargets();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllAsmPrinters();
llvm::InitializeNativeTarget();
llvm::InitializeAllAsmParsers();
// Some per-test setup I can make into its own function
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile...));
CHECK_NOTHROW(Interpret(...));
CHECK_THROWS(Compile(...));
CHECK_THROWS(Compile(...));
}
What I want is to refactor it into three TEST_CASE
,
- one for tests that should pass compilation,
- one for tests that should fail, and
- one for tests that should pass interpretation (and in the future, further such divisions, perhaps).
But I can't simply move the test contents into another TEST_CASE
because if that TEST_CASE
is called before the one that sets up the inconvenient globals, then they won't be initialized and the testing will spuriously fail.
I'm a bit late to this as I only just saw it - sorry (in future you can post Catch related questions to the Catch forum or issues list on GitHub, if appropriate.
Anyway - I don't know what you did in the end but in this case it sounds like you just want to group each set of assertions into SECTION
s.
TEST_CASE() {
// Initialize really shitty LLVM global variables.
llvm::InitializeAllTargets();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllAsmPrinters();
llvm::InitializeNativeTarget();
llvm::InitializeAllAsmParsers();
SECTION( "should pass compilation" ) {
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile(...));
CHECK_NOTHROW(Compile...));
}
SECTION( "should pass interpretation" ) {
CHECK_NOTHROW(Interpret(...));
}
SECTION( "Should fail compilation" ) {
CHECK_THROWS(Compile(...));
CHECK_THROWS(Compile(...));
}
}
Each section then acts like an embedded test case (the whole test case is executed from the start - through all the initialisation - for each section). So if one of the no-throws throws it will not prevent the other sections from executing.
... unless the initialisation code should only be executed once - in which case you could either put in a static initialiser, as @paddy suggested (a class that calls the initialisers in its constructor - then just create a global instance) - or you could protect the block of initialisation code with an if on a static bool.
这篇关于抓住测试用例顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!