<分区>
为 C++ 项目编写测试套件的最佳实践/指南是什么?
<分区>
为 C++ 项目编写测试套件的最佳实践/指南是什么?
最佳答案
这是一个非常宽泛的问题。对于单元测试和测试驱动开发 (TDD),此 from Microsoft 上有一些有用的(如果部分内容有点白)指南- 如果不适用,您可以忽略特定于 Visual Studio 的建议。
如果您正在寻找有关系统或性能测试的指导,我会澄清您的问题。 docs for Boost.Test 中有一个更广泛的合理理由。 .
There are several unit testing best practices to review before we close. Firstly, TDD is an invaluable practice. Of all the development methodologies available, TDD is probably the one that will most radically improve development for many years to come and the investment is minimal. Any QA engineer will tell you that developers can't write successful software without corresponding tests. With TDD, the practice is to write those tests before even writing the implementation and ideally, writing the tests so that they can run as part of an unattended build script. It takes discipline to begin this habit, but once it is established, coding without the TDD approach feels like driving without a seatbelt.
For the tests themselves there are some additional principals that will help with successful testing:
Avoid creating dependencies between tests such that tests need to run in a particular order. Each test should be autonomous.
Use test initialization code to verify that test cleanup executed successfully and re-run the cleanup before executing a test if it did not run.
Write tests before writing the any production code implementation.
Create one test class corresponding to each class within the production code. This simplifies the test organization and makes it easy to choose where to places each test.
Use Visual Studio to generate the initial test project. This will significantly reduce the number of steps needed when manually setting up a test project and associating it to the production project.
Avoid creating other machine dependent tests such as tests dependent on a particular directory path.
Create mock objects to test interfaces. Mock objects are implemented within a test project to verify that the API matches the required functionality.
Verify that all tests run successfully before moving on to creating a new test. That way you ensure that you fix code immediately upon breaking it.
Maximize the number of tests that can be run unattended. Make absolutely certain that there is no reasonable unattended testing solution before relying solely on manual testing.
关于c++ - 编写测试套件的指南,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3978275/