Home > Enterprise >  Automated unit tests build and run on commandline with Visual Studio solution
Automated unit tests build and run on commandline with Visual Studio solution

Time:12-04

I'm working on a project with multiple Unit Tests. I have a visual studio .sln file with around 10 XXPrj in it. Those projects are made with Google Test. Everything works well if I want to run them using Visual Studio 2019, I can build and run the unit tests.

I would like to know what is the best way to run them an automated way with commandline. Purpose is to then integrate this commandline stuff in a jenkins to have everything automated.

CodePudding user response:

Build

Building a Visual Studio solution/project through the command line is done with msbuild.exe. It works best to add the path of MSBuild to the PATH environment variable.

MSBuild is usually installed somewhere in the Visual Studio folders. E.g. on my machine the path is as follows:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe

Build the solution containing all your projects as follows:

msbuild.exe Example.sln

# Or if you want to build a release version or add additional arguments
msbuild.exe Example.sln /property:Configuration=Debug

See MSBuild CLI Docs for more options.

Side Note: Jenkins has a msbuild plugin you can use with a build step called "Build a Visual Studio project or solution using MSBuild" (Important: this does not install MSBuild, it only provides a GUI to use MSBuild in a build plan).

Run Tests

To run the tests you have two options:

  • Run each project's executable in your build pipeline and the executable's exit code will indicate the success/failure of the unit tests for that project. However, you will need to call each executable separately; or
  • Use the vstest.console.exe in combination with the Google Test Adapter

You can use the Google Test Adapter the same way in which Visual Studio uses it when you click Test -> Run -> All tests to discover & execute tests in your projects.

In my environment, vstest.console.exe is located here:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\TestPlatform\vstest.console.exe

You also need to provide the path to the test adaptor. Then execute all the tests as follows:

# Assuming vstext.console.exe is included in the PATH
# and the current working directory is the relevant project executable
# output folder:
vstest.console.exe Project1.exe Project2.exe Project3.exe /TestAdapterPath:"<path to adapter>"

Once again the path is hidden somewhere in the Visual Studio Folders. I found it through searching for GoogleTestAdapter.TestAdapter.dll. On my machine it is located in:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\drknwe51.xnq

Conclusion

Thus, an automated way to build and run GoogleTest unit tests, which are split over multiple projects, with the commandline can be performed in these two steps:

  1. Build the solution/projects using msbuild.exe
  2. Run the tests using vtest.console.exe in combination with the Google Test Adapter
  • Related