I'm trying to test parts of my code. I wrote the following test.h file:
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(my_test) {
BOOST_CHECK(true);
}
If I run the test, my application's main method is invoked and since the command line arguments are missing, it terminates. I want to just run the test suite as it is and succeed since BOOST_CHECK on true should be a passed test. Once this works, I would add calls to functions from my code base one by one for regression testing. Is this possible to do? If yes, how?
This post suggests adding the following define to the top of the test.h file but it does not work for skipping the main method invocation:
#define BOOST_TEST_NO_MAIN true
BOOST_TEST_NO_MAIN makes Boost.Test omit its own main function, therefore it will fall back to the applications main function.
In you unit tests, do not link the applications main function (do not add the file which contains the main), and let Boost.Test add its own main, which will run all your tests.
Related
The built-in unit testing functionality (unittest {...} code blocks) seems to only be activated when running.
How can I activate unit tests in a library with no main function?
This is somewhat related to this SO question, although the accepted answer there deals with a workaround via the main function.
As an example, I would expect unit testing to fail on a file containing only this code:
int foo(int i) { return i + 1; }
unittest {
assert(foo(1) == 1); // should fail
}
You'll notice I don't have module declared at the top. I'm not sure if that matters for this specific question, but in reality I would have a module statement at the top.
How can I activate unit tests in a library with no main function?
You can use DMD's -main switch, or rdmd's --main switch, to add an empty main function to the set of compiled source files. This allows creating a unit test binary for your library.
If you use Dub, dub test will do something like the above automatically.
QTest encourages you to organize unit tests as separate executables. There is special macro for this, that generates the main function: QTEST_MAIN.
I found this approach not very clean, it is much more useful to run all tests at once. So i searched if there is any possibility of doing so and I found a few people proposing the same solution:
Qt: run unit tests from multiple test classes and summarize the output from all of them
http://www.davideling.it/2014/01/qtest-multiple-unit-test-classes/
https://alexhuszagh.github.io/2016/using-qttest-effectively/
The solution was to give up using QTEST_MAIN macro and write your own main function where you execute the tests you want to execute:
int main(int argc, char *argv[])
{
int status = 0;
{
TestA ta;
status |= QTest::qExec(&ta, argc, argv);
}
{
TestB tb;
status |= QTest::qExec(&tb, argc, argv);
}
return status;
}
I found it to be a great idea, however, there is a problem. Qt's documentation for qExec has a part that sounds like this:
For stand-alone test applications, this function should not be called
more than once, as command-line options for logging test output to
files and executing individual test functions will not behave
correctly.
The solution revealed by those people suggest just that: executing qExec more than once. Can anyone explain to me what exactly command-line options for logging test output to files and executing individual test functions will not behave correctly exactly mean?
What exactly could go wrong with this approach?
The documentation is probably talking about Logging Options. If you call qMain twice and pass the -o option to both calls, the second call will probably overwrite the log file from the first call. If you know that this will never happen you might choose to ignore the warning. You can also not pass the command line arguments to qExec, that way you will force the output to stdout, but you lose the ability to pass other arguments, of course.
If you want to run the test cases from Qt Creator, you should also not call qExec more than once. Each test class will show up in the test list, but running one will just run all, so you get the result for every class displayed for one class. And if you run all tests (the default), you get a the squared amount of results.
So if you don't like the multiple executable approach, just use Google Test. It has none of the above problems and the Creator provides support for it. The setup is very easy: A wizard will guide you when you create an Autotest project. The only thing you have to do is download Google Test.
Google test cases will show up right next to Qt Test cases in the test views.
I often wrote my test code in the main function while developing an API but because D has integrated unittest I want to start using them.
My current work flow is the following, I have a script that watches for file changes in any .d files, if the scripts finds a modified file it will run dub build
The problem is that dub build doesn't seem to build the unittest
module foo
struct Bar{..}
unittest{
...
// some syntax error here
...
}
It only compiles the unittests if I explicitly run dub test. But I don't want to run and compile them at the same time.
The second problem is that I want to be able to run unittests for a single module for example
dub test module foo
Would this be possible?
You can program a custom test runner using the trait getUnittests.
getUnitTests
Takes one argument, a symbol of an aggregate (e.g. struct/class/module). The result is a tuple of all the unit test functions of that aggregate. The functions returned are like normal nested static functions, CTFE will work and UDA's will be accessible.
in your main() you should be able to write something that takes an arbitrary number of module:
void runModuleTests(Modules...)()
{
static if (Modules.length > 1)
runModuleTests!(Modules[1..$]);
else static if (Modules.length == 1)
foreach(test; __traits(getUnitTests, Modules[0])) test;
}
of course the switch -unittest must be passed to dmd
I am writing unit tests for my golang code, and there are a couple methods that I would like to be ignored when coverage is calculated. Is this possible? If so, how?
One way to do it would be to put the functions you don't want tested in a separate go file, and use a build tag to keep it from being included during tests. For example, I do this sometimes with applications where I have a main.go file with the main function, maybe a usage function, etc., that don't get tested. Then you can add a test tag or something, like go test -v -cover -tags test and the main might look something like:
//+build !test
package main
func main() {
// do stuff
}
func usage() {
// show some usage info
}
I want to create some tests with boost::unit_test for my parallel (mpi based) C++ codes. I have some basic experiences in using the test framework. For me the main problem, when going to parallel codes, is where to put MPI::Init, such that it is called first. In the test suites I have created there is no main function. Furthermore, does Boost::Test exists correctly (with respect to mpi) when some assertions fail on a subset of the existing ranks?
Boost Test has fixture support, which allows you to perform setup/cleanup per test case, test suite, or globally. Sounds like you should put the call to MPI::Init in a global fixture.
struct MPIFixture {
MPIFixture() { MPI::Init(); }
~MPIFixture() { /* I bet there's a deinit you should call */ }
};
BOOST_GLOBAL_FIXTURE(MPIFixture);
If you have trouble working with that, or if you are working in a framework that provides its own main function, then you can #define BOOST_TEST_NO_MAIN before including the Boost headers. Then you can invoke boost::unit_test::unit_test_main yourself to run your test suites.