Adding unit tests to an existing project

房东的猫 提交于 2019-12-04 03:29:03

If your test app is only linking the object files it needs to test then you are effectively already treating them as a library, it should be possible to group those object files into a separate library for the main and the test app. If you can't then I don't see that what you are doing is too bad an alternative.

If you are having to link other object files not under test then that is a sign of dependencies that need to be broken, for which you have the perfect book. We have similar problems and use a system like the one suggested by Vlion

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.

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

I personally would continue doing as you are doing or consider having a build script that makes the target application and the unit tests at the same time (two resulting binaries off the same codebase). Yes it smells fishy but it is very practical.

Kudos to you and good luck with your testing.

I prefer one test executable per test. This enables link-time seams and also helps allow TDD as you can work on one unit and not worry about the rest of your code.

I make the libraries depend on all of the tests. Hopefully this means your tests are only run when the code actually changes.

If you do get a failure the tests will interrupt the build process at the right place.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!