Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding unit tests to an existing project

My question is quite relevant to something asked before but I need some practical advice.

I have "Working effectively with legacy code" in my hands and I 'm using advice from the book as I read it in the project I 'm working on. The project is a C++ application that consists of a few libraries but the major portion of the code is compiled to a single executable. I 'm using googletest for adding unit tests to existing code when I have to touch something.

My problem is how can I setup my build process so I can build my unit tests since there are two different executables that need to share code while I am not able to extract the code from my "under test" application to a library. Right now I have made my build process for the application that holds the unit tests link against the object files generated from the build process of the main application but I really dislike it. Are there any suggestions?

like image 993
Yorgos Pagles Avatar asked Oct 21 '08 20:10

Yorgos Pagles


People also ask

How do I add unit tests to an existing Xcode project?

To add a unit test target to an existing Xcode project, choose File > New > Target. Select your app platform (iOS, macOS, watchOS, tvOS) from the top of the New Target Assistant. Select the Unit Testing Bundle target from the list of targets.

Should unit tests be in the same project?

Put Unit tests in the same project as the code to achieve better encapsulation. You can easily test internal methods, which means you wont make methods public that should have been internal. Also it's really nice to have the unit tests close to the code you're writing.

How would you implement JUnit for an existing project?

To use JUnit you must create a separate . java file in your project that will test one of your existing classes. In the Package Explorer area on the left side of the Eclipse window, right-click the class you want to test and click New → JUnit Test Case. A dialog box will pop up to help you create your test case.


2 Answers

Working Effectively With Legacy Code is the best resource for how to start testing old code. There are really no short term solutions that won't result in things getting worse.

like image 97
cynicalman Avatar answered Oct 12 '22 07:10

cynicalman


I'll sketch out a makefile structure you can use:

all: tests executables

run-tests: tests
    <commands to run the test suite>

executables: <file list>
    <commands to build the files>

tests: unit-test1 unit-test2 etc

unit-test1: ,files that are required for your unit-test1>
    <commands to build unit-test1>

That is roughly what I do, as a sole developer on my project

like image 29
Paul Nathan Avatar answered Oct 12 '22 06:10

Paul Nathan