![]() The assertions are harder as we have to assert on a list as opposed to a single value. The setup of the test is harder, as we have to create a list as opposed to a single value. The code initially calculates values for a list of steps, which means that any test for it must take on this complication. This means that fewer tests are needed, and that the tests become small and simple enough to include the calculations. The refactorings reduce the number of paths through the code and simplify the test. These build on the existing test refactorings from XUnit Test Patterns. It doesn’t include the calculations, and introducing them would make the test even longer and more complicated, so would be hard to justify.Įach of the following sections describes a refactoring, and includes links to the refactored example code. This example code has an initial naive test, which is around 30 lines long. There are a few if statements and a loop, and in total these lead to 2^9, or 512, paths through the code! Eek! It is clearly not feasible (or useful) to test all these, hence the need to find ways to make it easier. But if not, don’t worry, the rest of this post will still make sense. The calculation is around 30 lines long, and is worth a quick glance now if you have the time. The rest of this article will discuss the example ConstructionMarginCalculator class, and will describe refactorings that simplify the tests and allow them to feasibly include the calculations. This article references the excellent XUnit Test Patterns book, which defines the most widely accepted lexicon on unit test patterns and practices. Sut.wake_erosion_rate(ambient_turbuluence) You might also want to replace the 2.5 and 0.05 with the names of the constants they represent. However, if you remove this code smell, and include the calculation in the test, it becomes obvious what the problem is when there is a failure, and the test does now act as documentation. The test tells you very little about how the system under test (SUT) should behave, and so doesn’t act as documentation. Including the calculations in the test is a big part of this, and avoids the Hard Coded Test Data smell.įor example, if a test looks like this, it is hard to know what the problem is when it fails, as the test data is all hard coded. It is a good idea to make tests as descriptive as possible – to achieve Tests as Documentation.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |