This guide describes how to quickly construct C++ projects for test automation on Windows or Linux.
- C++ project templates to construct projects for test automation
Giving a quick review of the folder structure used in this repository here, and hopefully it could be helpful for managing cross platform C++ projects.
$(RepoDir) // Local path of this repository
|-- bin // Put helper tools here
|-- cc // Put .cc or .h files here
| |-- src // Put source files here
| `-- test // Put test files here
|-- include // Put module headers here
|-- prj // Put project files here
| |-- pycharm // PyCharm projects
| |-- qmake // qmake projects
| `-- v141 // Visual Studio 2017 projects
|-- profile // Scripts to instrument and profiling binaries are put here.
|-- submod // Put submodule here
|-- build.py // A python script to build project and run test cases.
- Python: 3.6
- Visual Studio Enterprise: 2013 or later is required, and we are using 2017 here.
- Test Adapter
- Test Adapter for Google Test: This is an extension bundled with Visual Studio since 2015, and check it while you are installing Visual Studio.
- Google Test Adapter: Try this if you are using Visual Studio 2013 or earlier.
The first 4 commits in the Git log messages of this repository are the steps to construct C++ projects for test automation.
- 756bdfd7404 is to give a simple DLL project which include one simple API as an target binary for testing and profiling.
- 7e724d75ae2 is to add unit tests. We use Google Test as C++ test framework. It only needs few steps to setup a unit test project.
- Create a Visual C++ project of an empty console application named, for example,
$(TestProject). - Git clone Google Test as a submodule at
$(RepoDir)submod/googletest. - (Optional) Git switch to an official release commit, for example,
release-1.8.1. - Add the property sheet
googletest.propsto$(TestProject). - Use
main.ccandtest_division.ccas template for main function and test cases, then add them to$(TestProject).
- Create a Visual C++ project of an empty console application named, for example,
- (Optional) 9ab1e63cce8 is to configure projects being profiled. Open up project property and:
- Property -> Linker -> General -> Enable Incremental Linking -> No
- Property -> Linker -> Advanced -> Profile -> Yes
- Now we could click Test -> Analyze Code Coverage -> All Tests for running unit test
- Results would be shown on Code Coverage Results window.
- 070fb8cdc78 is to add a
build.pyto build modules and validate modules.
- Analyze code coverage for Windows service.
- Build binaries (DLL or EXE) with following linker options.
- Property -> Linker -> General -> Enable Incremental Linking -> No
- Property -> Linker -> Advanced -> Profile -> Yes
- Instrument binaries.
$(RepoDir)/profile/instrument.cmdis an example of instrumentation. - Start Profile Monitor.
$(RepoDir)/profile/monitor_start.cmdis an example to startVSPerf.- 2 options are needed to monitor Windows service.
/CrossSessionAllows client access to the monitor from another session./USERAllows client access to the monitor from the specified account
- 2 options are needed to monitor Windows service.
- Launch and test services with instrumented binaries.
- Stop services.
- Stop Profile Monitor.
$(RepoDir)/profile/monitor_stop.cmdis an example to stopVSPerf - Open .coverage file with Visual Studio Enterprise.
- Build binaries (DLL or EXE) with following linker options.
- Multiple results could be imported and merged. This is useful when some cases could be only tested on specific platform.
- Customize code coverage analysis.
- Compose run setting file to customize code coverage analysis. An example is
$(RepoDir)prj/v141/test_division/test_division.runsettings. - Customize by
VSInstr /INCLUDE:funcspec[;funcspec]*.
- Compose run setting file to customize code coverage analysis. An example is
This template project is developed on Ubuntu 18.04. However, any other Linux distribution should be fine. The sample commands to install the applications as listed as below.
> sudo apt install g++- The version of g++ used in this project is 7.3.0, and 5.2 or later should be good enough to compile this project.
> sudo apt install gcovr- The version of gcovr used in this project is 3.4.
- (Optional)
> sudo apt install python3- The version of Python used in this project is 3.6. This is required for running the Python scripts in this project. This is not required if you are not interested in running those scripts.
- Qt Creator - It's recommended to use official installer to install Qt Creator. Download install from here and make sure one of Qt libraries is selected. This is for compiler kit for save us time to configure development environment. The version of Qt library used in this project is 5.12.2, and the version of Qt Creator
Before going forward, we are assuming audiences are already familiar with Quick-start Steps for Windows. Especially how we configure Google Test.
- Open
division.proandtest_division.proin Qt Creator. division.pro: The qmake project to build our demonstrating project which include a application binary interface.test_division.pro: The qmake project to build a test project.- Ensure
INCLUDEPATHis well set, meaning that headers, source, and the dependencies, such as Google Test are well included. - Ensure
-lpthreadis included inLIBS.pthreadis a dependency of Google Test. -ldlis fordlopen/dlsym/dlcoseused in test code.
- Ensure
- 3fa4588798b is to revised
division.handtest_division.ccfor making sources could be built cross Visual C++ and G++. - Build all projects. Tests are now listed in the Test Explorer of Qt Creator.
- In case you don't see Test Explorer, click
Window -> Show Right Sidebar -> Teststo open it.
- In case you don't see Test Explorer, click
gcov is a test coverage program bundle with gcc compiler. If compiler and linker options are well configured, the code coverage data could be collected during the execution of program. It's recommended to use gcovr for analyzing code coverage results since it support to generate XML file for Cobertura Plugin. It's more useful if we'd like to integrate code coverage analysis into Jenkins continuous integration system.
- Ensure below options are well configured for the projects you want to collect code coverage data.
QMAKE_CXXFLAGS += --coverageQMAKE_LFLAGS += --coverage
- Runt test case or manually test programs.
- Analyze the data with tools like lcov or gcovr.
$(RepoDir)/profile/gcovr.shis a shell script to use gcovr to analyze code coverage results.$(RepoDir)/profile/lcov.shis a shell script to use lcov to analyze code coverage results.
In this section, few quick steps would be given to make Jenkins to take results of Google Test to generate test reports. We don't describe how to use Jenkins here, or we'll be distracted.
- Jenkins ver. 2.164.1 is used in this guide.
- Install xUnit plugin.
- Project -> Configure -> Post-build Actions -> Publish xUnit test result report.
- Refer to 4dcaf1f2a28 for how we generate report here.
- Set relative path to the XML file generated by Google Test.
- Click "Build Now" and see if the report is generated.
- Google Test on Bamboo or Jenkins.
- xUnit plugin could be used to work with Google Test to have test results on Jenkins.
- Code coverage analysis on Bamboo or Jenkins.
- Cobertura Plugin could be used as receiving reports generated from gcovr.
- Google Mock - A framework for writing and using C++ mock classes.
- Measure app performance in Visual Studio
- Testing tools in Visual Studio
- gcov - A coverage testing tool.
- gcovr - Generate code coverage reports with gcc/gcov
- lcov - An extension of GCOV to generate code coverage reports.
- Qt and qmake Manual