Experience of using doctest

Keywords: C++ unit testing Visual Studio

        There are many unit testing frameworks, and it is convenient for ubuntu to use gtest. The window s platform is mainly developed using vs2017. I use VS to develop qt projects. Although there are many fewer unit testing configurations with VS, I still need to import. props files. During testing, I need to change the application project into a static library project. My qt. props configuration is only imported into include, The include in the project needs a level-1 directory. Later, I found the doctest lightweight testing framework on the Internet. You can see the detailed introduction C + + unit test tool -- doctest_liao20081228 blog - CSDN blog_ doctest , very detailed.
        The import of doctest is very simple. You only need to put the doctest.h file into the project directory and use #include"doctest.h" instead of other configurations.

        I think the biggest advantage of doctest is that there is no need to create a new test project, especially for many projects with third-party libraries. Every time the environment is configured, the test code and source code are in the same file.

        Because the doctest test code and the production code are placed in the same cpp file, the article says that you can also write another test file like gtest, but this gives up the advantage of doctest. I hope that during the formal compilation, the test code can be removed. Look at the document and say that doctest is defined_ CONFIG_ The disable identifier removes all test related content from the binary executable. When I use it, I define doctest_ CONFIG_ After disable, it will compile and report errors, and no good solution is found. My solution is to customize__ DOCTEST_DISABLE macro, and then test the code using #ifndef__DOCTEST_DISABLE and #endif include.

        In this way, the code of the project in main.cpp is

#include "mainWin.h"
#include <QApplication>
#include <vtkOutputWindow.h>
#include "def.h"
#define DOCTEST_CONFIG_IMPLEMENT
#include "doctest.h"


int main(int argc, char *argv[])
{
	//Remove vtk error reporting window
	//vtkOutputWindow::SetGlobalWarningDisplay(0);

	QApplication a(argc, argv);
	MainWin w;
	w.show();
	w.showMaximized();

#ifndef __DOCTEST_DISABLE
	doctest::Context context;
	context.applyCommandLine(argc, argv);
	context.setOption("order-by", "name");
	int res = context.run(); // run doctest

	if (context.shouldExit()) {
		// propagate the result of the tests
		return res;
	}
#endif // !__DOCTEST_DISABLE

	int ret = a.exec();

	return ret;
}

be careful  # define DOCTEST_CONFIG_IMPLEMENT should be preceded by #include "doctest.h", and the def.h file is

#ifndef __DEF_H__
#define __DEF_H__
//#define DOCTEST_CONFIG_DISABLE
//#define __DOCTEST_DISABLE
#endif //__DEF_H__

When you remove the test code, remove the two macro comments.

In the class of reading and writing json files for singleton, test data reading and writing. The. cpp file to be tested contains "def.h" and "doctest.h"

#include "def.h"
#include "doctest.h"

#ifndef __DOCTEST_DISABLE
TEST_CASE("testing SparaSet function") {
	bool ret = CONFIG->ReadJsonFile("D:\\data\\test\\config.json");
	REQUIRE(ret);

	
	SUBCASE("test getSpara double") {
		CONFIG->setSpara("test", "pi", 3.14);
		CONFIG->setSpara("pi", 3.14);
		double pi = CONFIG->getSpara("test", "pi").toDouble();
		CHECK(pi== doctest::Approx(3.14));
		pi = CONFIG->getSpara("pi").toDouble();
		CHECK(pi == doctest::Approx(3.14));

		double zero = CONFIG->getSpara("test", "zero").toDouble();
		CHECK(zero == doctest::Approx(0));
		
	}

	SUBCASE("test getSpara string") {
		QVariant str("strTest");
		CONFIG->setSpara("test", "str", str);
		CONFIG->setSpara("str", str);

		QString getStr = CONFIG->getSpara("test", "str").toString();
		CHECK(getStr.compare(str.toString()) == 0);
		getStr = CONFIG->getSpara( "str").toString();
		CHECK(getStr.compare(str.toString()) == 0);

		getStr = CONFIG->getSpara("test", "empty").toString();
		CHECK(getStr.isEmpty());

	}
}
#endif

In the common.cpp file

#include "def.h"
#include "doctest.h"

#ifndef __DOCTEST_DISABLE
TEST_CASE("testing common function") {
	string file = "D:\\data\\test\\1.txt";
	string noexist = "D:\\data\\test\\none.txt";
	string empty = "";
	SUBCASE("test IsExist") {
		CHECK(Common::IsExist(file.c_str()));
		CHECK_FALSE(Common::IsExist(noexist.c_str()));
		CHECK_FALSE(Common::IsExist(empty.c_str()));
	}

}
#endif

I set the subsystem as the console, so after I run the test code, the results are as shown in the figure

          For me, doctest has been able to meet my unit testing needs. For more features, please see the official doctest documentation.

Posted by juma929 on Sun, 26 Sep 2021 18:13:53 -0700