Trying to run some tests through the use of the boost test library by using the following code:
int main(int arg, char *argv[])
{
string help = "help";
#ifdef TESTING_CONSTANT
TestingPeerClass testingClass;
testingClass.test_method();
#else
//rest of the codre
#endif
}
Which calls the following test function found in another class called testing.h :
#define BOOST_TEST_MODULE mytest
#include <algorithm>
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(TestingPeerClass)
{
BOOST_CHECK(1 == 1);
}
when running the test a strange runtime error raises when it hits the BOOST_CHECK function.
I cant figure out why, any ideas? since when i built a new project it worked perfectly fine.
Related
In an attempt to incorporate a windows platform feature into an otherwise crossplatform application, I've made a one-function VC++ DLL in visual studio that uses Windows.Management.Deployment.PackageManager to get some details on all installed windows store apps.
The function works fine as a standalone application, and I can successfully build the DLL with MSVC that links properly with my MinGW main application (I can happily return primitive data from the dll, etc) - but any attempt to execute a function from the dll containing code relating to PackageManager crashes my application in runtime with the unhelpful code -529697949.
Here's some minimal code blocks that replicate:
main.cpp in the main application:
#include <QCoreApplication>
#include "mylib/WindowsAppsLib.h"
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
auto hi = (Helpers::sayHi());
qDebug() << (hi);
return a.exec();
}
dll header:
#pragma once
#define WINDOWSAPPSLIB_API __declspec(dllexport)
namespace Helpers
{
extern "C" WINDOWSAPPSLIB_API const char* sayHi();
}
dll source:
#include "stdafx.h"
#include <sddl.h>
#include <collection.h>
#include "WindowsAppsLib.h"
#include <windows.h>
#using <Windows.winmd>
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace std;
const char* Helpers::sayHi()
{
auto packageManager = ref new Windows::Management::Deployment::PackageManager();
auto packages = packageManager->FindPackages();
return "Hi!";
}
Without the two lines relating to packagemanger, the program runs fine and prints "Hi!". When included, the program crashes with code -529697949 as soon as sayHi() is called. The two lines in themselves have their dependencies available and don't cause exceptions.
Any clues on how I might proceed to investigate this? Nothing I've been able to get out of this system is getting me closer to identifying the problem. Is this the sensible way to access Windows.Management.Deployment.PackageManager from within a plain C++ MinGW application to begin with?
I'm testing a huge piece of software and would like to use Catch for this task.
I'm using the "single include" version 1.9, integrating it in Visual Studio 2012 update 4 and using C++04 standard.
As you will see below, I use three ".cpp" files. Each of them reference:
a include file providing "abstraction" macros (eg. #define Assert(x) REQUIRE(x));
a utility file providing... utilities for the test;
the specific test target include files;
some "using namespace" statement, all cpp files "using" the same namespace;
the actual tests, written with the macros (eg. Assert(2 == getNumber())).
More details on the files content below.
This configuration works, but one of the test file is growing bigger by the day and I would like to split it in 3, or more. Now, say that I do the following:
take part of the content of a test test.cpp and move it in a new file test2.cpp;
add the same includes and defines to make it compile;
include the new file in my main
this error pops up when I run the tests:
=============================
No tests ran
error: TEST_CASE( "test 2" ) already defined.
First seen at c:\tests\catchtest2\catchtest2\test2.cpp(3)
Redefined at c:\tests\catchtest2\catchtest2\test2.cpp(3)
where test2.cpp is the new file.
If I move the content back to test.cpp it all works, but working with tests thousands of lines long is almost harder than working on the project itself, and the dimension could grow 3, 4 times bigger still.
Is there a way to split the tests in multiple files?
-- NOTES --
I reckon including Catch in a header and using the including header instead of catch.cpp directly is not a good idea, but I successfully used this configuration with 3 (exactly 3) included .cpp test files, and am unable to use this with 4 files.
I remember reading that it was somehow related to the line at which the components were defined, but I can also move the code so that the test cases are defined at different lines and the behaviour doesn't change.
I also tried to "clean and rebuild", because it may well be that dirty data is kept in the compiler / linker's caches, but to no avail.
I couldn't create an actual MWE right now, so I gave you a sketch of the test setup as accurate as I thought it could be needed. I'm more than willing to provide additional details or try and build an actual MWE and share it.
Any idea is appreciated!
My "working" code looks like this:
main.cpp
#define CATCH_CONFIG_RUNNER
#include "catch.hpp"
#include "test1.cpp"
#include "test2.cpp"
#include "test3.cpp"
int main( int argc, char* argv[] )
{
cleanDir("c:\\temp");
init (argv);
int result = Catch::Session().run( argc, argv );
return ( result < 0xff ? result : 0xff );
}
test1.cpp
#include "unitTestSuite.h"
#include "utils.h"
#include "systemundertest.h"
using namespace system::under::test;
my_test_case("test 1") {
my_section("does X") {
// tests...
}
}
my_test_case("test 2") {
my_section("does Y") {
// tests...
}
}
unitTestSuite.h
#ifndef UNIT_TEST_SUITE
#define UNIT_TEST_SUITE 1
#include "catch.hpp"
#define my_test_case(x) TEST_CASE("Testing: " x)
... // here is also a namespace with some unit test specific utils
#endif
utils.h
#ifndef _UTILS_
#define _UTILS_
// some global variables declared here and defined in utils.cpp
// template functions defined in the header
// non-template functions defined in utils.cpp
// a test generation namespace with some template functions and some non-template functions defined in utils.cpp
#endif
After the "split":
test1.cpp
#include "unitTestSuite.h"
#include "utils.h"
#include "systemundertest.h"
using namespace system::under::test;
my_test_case("test 1") {
my_section("does X") {
// tests...
}
}
test1.2.cpp
#include "unitTestSuite.h"
#include "utils.h"
#include "systemundertest.h"
using namespace system::under::test;
my_test_case("test 2") {
my_section("does Y") {
// tests...
}
}
main.cpp
#define CATCH_CONFIG_RUNNER
#include "catch.hpp"
#include "test1.cpp"
#include "test1.2.cpp"
#include "test2.cpp"
#include "test3.cpp"
int main( int argc, char* argv[] )
{
cleanDir("c:\\temp");
init (argv);
int result = Catch::Session().run( argc, argv );
return ( result < 0xff ? result : 0xff );
}
program output:
=============================
No tests ran
error: TEST_CASE( "test 2" ) already defined.
First seen at c:\tests\catchtest2\catchtest2\test1.2.cpp(3)
Redefined at c:\tests\catchtest2\catchtest2\test1.2.cpp(3)
Don’t include the cpp files, just add them to the project.
Your main.cpp file only needs the first two lines (a define and an include).
I have a C++ project which is being tested using Catch.cpp:
I compile and run the following file to run my tests:
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "test1.h"
#include "test2.h"
In the near future I'll want to run more complex tests, that require command line arguments.
In Other words, I would like to apply some logic to my tests runs, being able to run them from inside loops or conditions and pass variables to them.
I found this page: Supply your own main.
which seems like a good direction but i could not find a more detailed explanation.
Can this be done by catch? or maybe there is a better way to implement what i described?
What I managed to do is to write my own main, parse the command line and store the needed parameter (in my case an IP) in a global variable. Not the most beautiful code, but does the job:
#define CATCH_CONFIG_RUNNER
#include "catch.hpp"
std::string IP;
int main(int argc, char* const argv[])
{
Catch::Session session;
int returnCode = session.applyCommandLine(argc, argv, Catch::Session::OnUnusedOptions::Ignore);
if (returnCode != 0)
return returnCode;
for (auto token : session.unusedTokens()) {
printf("Token: %s\n", token.data.c_str());
IP = token.data;
}
return session.run();
}
I then run this as TestRunner.exe [test-case-tag] --MY_IP_HERE. Note that without the "--" in front of the parameter, they don't appear in the unusedTokens.
I run this piece of code
#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include <boost/test/unit_test_log.hpp>
#include <boost/filesystem/fstream.hpp>
#include <iostream>
using namespace boost::unit_test;
using namespace std;
void TestFoo()
{
BOOST_CHECK(0==0);
}
test_suite* init_unit_test_suite( int argc, char* argv[] )
{
std::cout << "Enter init_unit_test_suite" << endl;
boost::unit_test::test_suite* master_test_suite =
BOOST_TEST_SUITE( "MasterTestSuite" );
master_test_suite->add(BOOST_TEST_CASE(&TestFoo));
return master_test_suite;
}
But at runtime it says
Test setup error: test tree is empty
Why does it not run the init_unit_test_suite function?
Did you actually dynamically link against the boost_unit_test framework library? Furthermore, the combination of manual test registration and the definition of BOOST_TEST_MAIN does not work. The dynamic library requires slightly different initialization routines.
The easiest way to avoid this hurdle is to use automatic test registration
#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include <boost/test/unit_test_log.hpp>
#include <boost/filesystem/fstream.hpp>
#include <iostream>
using namespace boost::unit_test;
using namespace std;
BOOST_AUTO_TEST_SUITE(MasterSuite)
BOOST_AUTO_TEST_CASE(TestFoo)
{
BOOST_CHECK(0==0);
}
BOOST_AUTO_TEST_SUITE_END()
This is more robust and scales much better when you add more and more tests.
I had exactly the same issue. Besides switching to automatic test registration, as suggested previously, you can also use static linking, i.e. by replacing
#define BOOST_TEST_DYN_LINK
with
#define BOOST_TEST_STATIC_LINK
This was suggested at the boost mailing list:
The easiest way to fix this is to [...] link with static library.
Dynamic library init API is slightly different since 1.34.1 and this is the
cause of the error you see. init_unit_test_suite function is not called in this
case.
I'm trying to test a library that I've done (Calculus), in QTCreator for Windows.
I've created a main file, and a class in a separate file for the testing. If I compile the example found in http://www.boost.org/doc/libs/1_47_0/libs/test/doc/html/utf/user-guide/test-organization/manual-test-suite.html it works, and so the example found in http://www.boost.org/doc/libs/1_47_0/libs/test/doc/html/utf/user-guide/test-organization/manual-nullary-test-case.html also works.
But when I try to compile my project I've a lot (over 500) errors of multiple definitions. Below you can find my files. As you can see I've also tried to put some guard around boost headers, but it does not work. What am I doing wrong?
main.cpp
#include "testcalculus.h"
#ifndef USE_BOOST_HEADERS
#define USE_BOOST_HEADERS
#include <boost/test/included/unit_test.hpp>
#include <boost/bind.hpp>
#endif
using namespace boost::unit_test;
test_suite*
init_unit_test_suite( int argc, char* argv[] )
{
WRayTesting::TestCalculus xTestCalculus;
test_suite* pxTestSuiteCalculus = BOOST_TEST_SUITE("Test Calculus");
pxTestSuiteCalculus->add(BOOST_TEST_CASE( boost::bind(&WRayTesting::TestCalculus::testCartesianPoint2D, &xTestCalculus)));
framework::master_test_suite().add(pxTestSuiteCalculus);
return 0;
}
testcalculus.h
#ifndef TESTCALCULUS_H
#define TESTCALCULUS_H
#ifndef USE_BOOST_HEADERS
#define USE_BOOST_HEADERS
#include <boost/test/included/unit_test.hpp>
#include <boost/bind.hpp>
#endif
#include "cartesianpoint2d.h"
#include "cartesianvector2d.h"
namespace WRayTesting
{
/** Class for testing the Calculus project */
class TestCalculus
{
public:
//! Constructor
TestCalculus();
//! Testing class point
void testCartesianPoint2D();
private:
};
} // namespace WRayTesting
#endif // TESTCALCULUS_H
testcalculus.cpp
#include "testcalculus.h"
#ifndef USE_BOOST_HEADERS
#define USE_BOOST_HEADERS
#include <boost/test/included/unit_test.hpp>
#include <boost/bind.hpp>
#endif
namespace WRayTesting
{
using ::Calculus::CartesianPoint2D;
using namespace boost::unit_test;
/**
* Constructor
*/
TestCalculus::TestCalculus()
{
}
/**
* Test the CartesianPoint2D class.
*/
void TestCalculus::testCartesianPoint2D()
{
// Default constructor
CartesianPoint2D xTestingPoint;
BOOST_CHECK(0.0 == xTestingPoint.getX());
BOOST_CHECK(0.0 == xTestingPoint.getY());
}
} // namespace WRayTesting
Compile output
debug/testcalculus.o:c:/lib/boost/boost/test/impl/compiler_log_formatter.ipp:62: multiple definition of `boost::unit_test::output::compiler_log_formatter::log_start(std::ostream&, unsigned long)'
debug/main.o:c:/lib/boost/boost/test/impl/compiler_log_formatter.ipp:62: first defined here
debug/testcalculus.o:c:/lib/boost/boost/test/impl/compiler_log_formatter.ipp:72: multiple definition of `boost::unit_test::output::compiler_log_formatter::log_finish(std::ostream&)'
debug/main.o:c:/lib/boost/boost/test/impl/compiler_log_formatter.ipp:72: first defined here
debug/testcalculus.o:c:/lib/boost/boost/test/impl/compiler_log_formatter.ipp:80: multiple definition of `boost::unit_test::output::compiler_log_formatter::log_build_info(std::ostream&)'
debug/main.o:c:/lib/boost/boost/test/impl/compiler_log_formatter.ipp:80: first defined here
debug/testcalculus.o:c:/lib/boost/boost/test/impl/compiler_log_formatter.ipp:93: multiple definition of `boost::unit_test::output::compiler_log_formatter::test_unit_start(std::ostream&, boost::unit_test::test_unit const&)'
debug/main.o:c:/lib/boost/boost/test/impl/compiler_log_formatter.ipp:93: first defined here
debug/testcalculus.o:c:/lib/boost/boost/test/impl/compiler_log_formatter.ipp:103: multiple definition of `boost::unit_test::output::compiler_log_formatter::test_unit_finish(std::ostream&, boost::unit_test::test_unit const&, unsigned long)'
debug/main.o:c:/lib/boost/boost/test/impl/compiler_log_formatter.ipp:103: first defined here
...........
You cannot include #include in multiple files within the same test module. You either need to switch to library or put everything inside single file