1

I want to establish "best-practices" as well as "reasonable baby-steps" for getting legacy projects to use unit-tests, modernize, and decouple unnecessary dependencies.

I found googletest to be a reasonable testing framework for writing unit tests. Getting it to work on existing code in visual studio is way too hard.

  1. Is it necessary for googletest to be its own project?

/fooApp/foo/foo.vcxproj
/fooApp/foo.test/foo.test.vcxproj

I found that this requires many changes for the test project to work.

  • .cpp files need to be added by hand with a relative path
  • precompiled headers and stdafx.h conflict with the test project
  • unresolved symbols require adding more code into the test project

    1. Can google test just be added to the existing project? As a build Configuration

Is this even possible? I'd much rather have this than make in a new project.

/foo/fooDoc.h
/foo/fooDoc.cpp
/foo/fooDoc.t.cpp

debug
release
unit test

Any guidance about using googletest in a visual cpp project would be appreciated. I can write tests, I just want the configuration to be less painful.

edit: Here are a few more examples of things I tried with varying success

  • put foo.test.vcxproj in same folder as the foo.vcxproj this will make various folders collide and fail
  • use the same stdafx.h for pre-compiled headers, requires adding a path the the include folder, requires changing both projects
  • have stdafx.h include pch.h, requires #ifdefs and other changes. ( missing win_sdk_ver)
  • getting no tests found error. starting over.
Ben L
  • 1,449
  • 1
  • 16
  • 32
  • I suspect that you have a single huge project which contains all the application code. And that that code consists of many translation units that are all interdependent and therefore are rather tightly bundled together so it is troublesome to use them in separate project. Right? – user7860670 Mar 11 '19 at 18:04
  • Some are huge and some projects are small. I'm finding a deficit of information about how to integrate gtest into a real project. I've only explored the separate project option and had so many issues. Most of them are not due to the interdependencies I intend to fix. Most are due to the Msbuild files, precompiled headers, and other such nonsense. – Ben L Mar 11 '19 at 18:21
  • *"due to the Msbuild files, precompiled headers, and other such nonsense"* - can you elaborate more on this? Which msbuild files are you talking about? And what is the deal with precompiled headers? Presence or absence of precompiled headers should make no difference. And it definitely should not make it more difficult to spawn a separate test project. – user7860670 Mar 11 '19 at 18:42
  • Is your project a library, or is it an executable file? Good practice to bring the entire functional part to the library (static or dynamic) and link it with the application. In this case, you just need to create a new test project, link it with googletest and your library. In the case of a running application, you will have to create a new test project and put all the necessary source files into it (without `main()`). – Dmytro Dadyka Mar 11 '19 at 19:08
  • Some are static libraries, some are dlls, and a few are .exe – Ben L Mar 11 '19 at 19:11
  • In any case, you need to create a new project within the solution. – Dmytro Dadyka Mar 11 '19 at 19:11
  • @BenL, Start by developing unit tests for libraries. It's simple - create a project, link it with your libraries and googletest library. – Dmytro Dadyka Mar 11 '19 at 19:13
  • @VTT I didn't elaborate on topics. In general, the issue is I make the separate test project via the wizard, then I fight with the configuration. "pch.h is missing from the .cpp file" "stdafx.h" cannot be found.... change the project to use stdafx.h as the precompiled headers.....add some include paths, get different errors...etc. – Ben L Mar 11 '19 at 19:16
  • Are you aware of [Property Manager](https://learn.microsoft.com/en-us/cpp/ide/working-with-project-properties?view=vs-2017#bkmkPropertySheets) existence? It seems like a common situation when people get unhappy with VS / msbuild because they waste time on manually configuring precompiled headers, includes and other settings for every project instead of just importing a couple of props. – user7860670 Mar 11 '19 at 19:33
  • @VTT I am aware of property manager and how msbuild works. I usually write my own .targets and .props systems. I still require the knowledge to find the "best practices" for how these systems should be configured. This is still a mystery for me. – Ben L Mar 11 '19 at 19:39
  • Best (and almost unavoidable) practice is *three* projects, as per [How to test an EXE with Google Test?](https://stackoverflow.com/q/23088252/1362568) – Mike Kinghan Mar 12 '19 at 11:40

0 Answers0