Summary of advantages (disadvantages) when switching from CUnit to Google Test

Target people and purposes of this page

It is intended for people who are using CUnit as a unit testing framework. ・ People who want to recommend Google Test to their surroundings ・ People who want to explain the usefulness to the people concerned because they want to switch to Google Test

It is intended for people who are looking for a test framework that is easier to use than CUnit, but I think the content is thin compared to other pages. I know Google Test to some extent and focus on how to recommend it to others.

I think it would be better to have a sample code, but I will upload it if there is a need.

What is Google Test

Google Test, like CUnit, is a unit testing framework. All the manuals are available online (though I think CUnit also has ones for this). http://opencv.jp/googletestdocs/primer.html

I have downloaded and used 1.8.0 from here. https://github.com/google/googletest/releases

merit

I have listed them in the order in which they are most effective from the developer's perspective. If you explain in this order, you will surely want to use it. Also, when explaining the usefulness to people other than developers, the point that "can be written short" from the perspective of the project manager is the man-hours for test maintenance, and the point that "installation is unnecessary" from the perspective of the environment manager is insisted. May be a benefit. We will also touch on the utilization of existing CUnit assets at the end of this page.

・ (Maybe) Can be written shorter than CUnit ・ I wrote a test case but never forgot to put it in ・ Can be used without installation ・ No main function required ・ A list of test cases can be output. ・ Only specified tests can be executed ・ Setup and TearDown functions can be prepared for each test suite (strictly called test fixture). -The disabled test will explicitly display "How many tests have been disabled" when the test is executed.

The following will be explained in order.

(Maybe) I can write shorter than CUnit, I wrote a test case but never forgot to put it

CUnit needs to add AddSuite-like processing after writing a test case, It's easy to make the mistake of writing it inadvertently but not testing it. Google Test will automatically include a test case in the test when you write it.

Can be used without installation

It provides all CPP files and headers, so if you include the include path settings and CPP in the build target, you can use it without installing it in / usr / local / lib. Useful when you want to give it a try and recommend it. The detailed method is mentioned in this article (http://d.hatena.ne.jp/E_Mattsan/20120215/1329311774) etc., but used-src is not currently included in the Release version, so the following is implemented I had to.

fuse_gtest_files.py

Of course, you can also install and use it.

main function not required

Since Google Test has prepared it, you can use it just by including it in the build target.

Can output a list of test cases, can execute only specified tests

See below for details. http://opencv.jp/googletestdocs/advancedguide.html#adv-listing-test-names http://opencv.jp/googletestdocs/advancedguide.html#adv-running-a-subset-of-the-tests

In CUnit, I have experience of partially #if 0 and cluttering where test cases are defined.

Setup and TearDown functions can be prepared for each test suite (strictly called test fixture).

See below for details. http://opencv.jp/googletestdocs/primer.html#primer-test-fixtures

What test cases in this test suite should be initialized first and what should be cleaned up? I think that readability will be improved because you can write that in the declaration of the test suite.

Disabled tests will explicitly display "How many tests have been disabled" when running the test.

See below for details. http://opencv.jp/googletestdocs/advancedguide.html#adv-temporarily-disabling-tests

If it is #if 0, it may be forgotten someday. However, in the case of "It will be NG because it is not implemented now. But I have made a test for the time being, so I want to test it once it is implemented" I think it's a good idea to set it to "explicit invalidation test".

Demerit

Requires knowledge of C ++, but only the ability to define classes and super rudimentary knowledge of extern "C". I can't think of any disadvantages with CUnit.

Other

Things that tend to get stuck when building

-Need to specify -lpthread -I have built the cpp source with gcc instead of g ++

Which feature should I use?

There seem to be various useful functions, but if the developer uses them too freely, tricky tests will be created or it will be out of control, so I think it is good to decide which functions should be used and which should not be used.

How to coexist with past CUnit assets?

What happens to test cases written in CUnit after migrating to Google Test? Do you throw it away? I think that the question will always be asked by the manager. From the conclusion, it is impossible to divert it with zero man-hours, but it can be used with a little mechanical modification. If you continue to make tests in the future, you can explain that it is an investment that can be recovered by the amount of increased production efficiency.

Some modifications need to be made to the CUnit-based test cases. I will write in detail if there is a need, but make a note of what I did.

Disable parts that are only needed in CUnit

Delete or disable the main function, AddSuite, AddTestCase processing, etc.

Rewrite test case functions based on Google Test

void testcase01(void){ To TEST(testcase01){ Or TEST_F(testName, testcase01){

This will be overloaded depending on the number of test cases. It's a mechanical work, so it seems that you can do it with a script etc., but if you can not devote to this replacement work, please give up ...

Replace macros for CUnit with Google Test macros

#define CU_ASSERT EXPECT_TRUE

Include CUnit files from CPP files for Google Test

#include "testCaseForCUnit.c"

Rewrite Makefile

For Google Test.

Finally

I wrote it while thinking that there was no need for it, so I think there are some points that are difficult to understand. Give me a sample code! I will update it if there is something like that. Please forgive me for the first post ...

Recommended Posts

Summary of advantages (disadvantages) when switching from CUnit to Google Test
Summary of vtkThreshold (updated from time to time)
Summary of gcc options (updated from time to time)
Points to note when switching from NAOqi OS 2.4.3 to 2.5.5
Summary of test method
[Updated from time to time] Summary of design patterns in Java
Sample code summary when working with Google Spreadsheets from Google Colab
How to avoid duplication of data when inputting from Python to SQLite.
[Recommendation] Summary of advantages and disadvantages of content-based and collaborative filtering / implementation method
vtkXMLUnstructuredGridReader Summary (updated from time to time)
vtkOpenFOAMReader Summary (Updated from time to time)
Summary of how to use pandas.DataFrame.loc
Summary of how to use pyenv-virtualenv
Summary of how to use csvkit
How to deal with OAuth2 error when using Google APIs from Python
When you want to change the HTTP headers of Flask's test client