This is a very broad question. For unit testing and Test Driven Development (TDD), there is some useful (if somewhat platidinous in parts) guidance on this from Microsoft - you can overlook the Visual Studio-specific advice, if it does not apply.
If you are looking for guidance on system or performance testing, I would clarify your question. There is a decent broader rationale in the 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.
TDD 无疑是一组最佳实践。在改造测试时,我的目标是代码覆盖率和边界条件覆盖率两件事。基本上,您应该选择函数的输入,以便 A) 所有代码路径都经过测试,如果所有代码路径的所有排列都经过测试,效果会更好(有时可能会出现大量情况,如果路径差异表面上不同,则实际上没有必要)并且B) 如果您的代码具有 if x > ,则测试所有边界条件(这些是导致代码路径选择变化的条件)。在其中的 5 中,您使用 x = 5 和 x = 6 进行测试以获得边界的两侧。
TDD is certainly one set of bests practices. When retrofitting tests, I aim for two things code coverage and boundary condition coverage. Basically you should pick inputs to functions such that A) All code paths are tested and better if all permutations of all code paths are tested(sometimes that can be a large number of cases and not really be necessary if path differences are superficially different) and B) That all boundary conditions(those are conditions that cause variation in code path selection) are tested if your code has an if x > 5 in it you test with x = 5, and x = 6 to get both sides of the boundary.
发布评论
评论(2)
这是一个非常广泛的问题。对于单元测试和测试驱动开发 (TDD),此来自 Microsoft - 如果不适用,您可以忽略特定于 Visual Studio 的建议。
如果您正在寻找有关系统或性能测试的指导,我会澄清您的问题。 Boost.Test 文档。
This is a very broad question. For unit testing and Test Driven Development (TDD), there is some useful (if somewhat platidinous in parts) guidance on this from Microsoft - you can overlook the Visual Studio-specific advice, if it does not apply.
If you are looking for guidance on system or performance testing, I would clarify your question. There is a decent broader rationale in the docs for Boost.Test.
TDD 无疑是一组最佳实践。在改造测试时,我的目标是代码覆盖率和边界条件覆盖率两件事。基本上,您应该选择函数的输入,以便 A) 所有代码路径都经过测试,如果所有代码路径的所有排列都经过测试,效果会更好(有时可能会出现大量情况,如果路径差异表面上不同,则实际上没有必要)并且B) 如果您的代码具有 if x > ,则测试所有边界条件(这些是导致代码路径选择变化的条件)。在其中的 5 中,您使用 x = 5 和 x = 6 进行测试以获得边界的两侧。
TDD is certainly one set of bests practices. When retrofitting tests, I aim for two things code coverage and boundary condition coverage. Basically you should pick inputs to functions such that A) All code paths are tested and better if all permutations of all code paths are tested(sometimes that can be a large number of cases and not really be necessary if path differences are superficially different) and B) That all boundary conditions(those are conditions that cause variation in code path selection) are tested if your code has an if x > 5 in it you test with x = 5, and x = 6 to get both sides of the boundary.