Open ghost opened 4 years ago
How about counting all of them?
#include "ut.hpp"
int main() {
using namespace boost::ut;
test("set") = []{
expect(sizeof(1) == 4); // Sanity check before nested tests.
test("one") = []{ expect(1==1); };
test("two") = []{ expect(2==2); };
};
}
$ ./test
All tests passed (3 asserts in 3 tests)
That would be a nice option.
What I would like to see is not that only "one' or "two" are executed, but either "one" or "two" are executed on a given path.
Imagine that both tests share some setup logic which needs to be replicated:
test("vector") = [] {
// prepare common part
std::vector v{1,2,3};
test("erase") = [&] {
v.erase(v.begin());
expect(v.size() == 2_u);
};
test("insert") = [&] {
v.insert(v.begin(), 0);
expect(v.size() == 4_u);
};
};
In this case there should be only 2 tests: vector.erase
and vector.insert
, however currently UT treats them as one test:
Running "vector"...
"erase"...
"insert"...
<source>:16:FAILED [3 == 4]
FAILED
===============================================================================
tests: 1 | 1 failed
asserts: 2 | 1 passed | 1 failed
It might seem redundant for a simple vector, but tests may share quite complex initialization logic which diverts quite deep in a flow. In other words copying variables into lambda is not always in option.
Currently one should either copy-paste initialization code (which is really hard to maintain when system evolves) or move common steps into functions (which makes it much harder to see what the test really does).
My guess is that technically this can be done by marking tests as leaves (maybe with new keyword) and do only one leaf in a time (per level or maybe per some label). I.e. run to first leaf, mark it as done, then skip/count other leaves. If there are any, restart the suite, skipping already "done" leaves.
It should be semantically equivalent to
test("vector") = [](std::string_view which) {
// prepare common part
std::vector v{1, 2, 3};
if (which == "erase")
{
test("erase") = [&] {
v.erase(v.begin());
expect(v.size() == 2_u);
};
}
if (which == "insert")
{
test("insert") = [&] {
v.insert(v.begin(), 0);
expect(v.size() == 4_u);
};
}
} | std::vector{"erase"sv, "insert"sv};
All tests passed (2 asserts in 2 tests)
Nested tests
Expected Behavior
A file
test.cpp
containsCompilation and execution is
Actual Behavior
All other things being equal,
Steps to Reproduce the Problem
Project Directory Structure
See Expected Behavior.
Specifications
Further Implications
In BDD syntax, the standard output is undesirable. For example, something like
can result in the output
Suggestion
Add an optional parameter to the test function that will cause it to be counted as a test:
Then, one could apply it as follows
Employ a more elegant solution or update the tutorial if my understanding is off.
Thanks!