Skip to content

Contract Checks: Once and Only Once for Unit Testing (Part II)

ADMIN | 01-02-2011

The solution, as I mentioned in my preceding post about contract checks, is to move as many assertions as make sense into contract checks.

Hexagon pattern 1

In Part I of this post, I pointed out a problem with xUnit tests: it is difficult to obey Once and Only Once when the validation assertions are outside the method(s) being tested.  The solution, as I mentioned in my preceding post about contract checks, is to move as many assertions as make sense into contract checks. This has many benefits, most of which address the principles Stef was looking for (and the frustrations she encountered in applying them to testing Spatial's 3D InterOp product) in her post:

  1. Once and Only Once – by turning validation code outside a method into contract code within the method, the validation code is only written once (inside the method). Every test which calls the method will pass through the validation code; there is no need for complicated architecture in the test system to avoid duplication.
  2. Configuration Coverage – contract checks are performed whenever the method they check is called, including acceptance/system tests and runs of the actual application (if desired). In other words, they’re run as part of Stef’s “monolithic, mysterious test suite” (MMTS). In contrast, validation code is only called in the test(s) in which it is explicitly included by the test author(s). This means that contract checks are applied to many more data configurations than validation code, which in turn yields a greater return on the investment of writing the contract check.
  3. (No Need For) Cleverness – one of the difficulties in designing good tests is thinking of weird corner cases of the input data; Stef’s experience was that no-one is clever enough to think of the full weirdness of realistic customer data. Because contract checks are run whenever the method is called, they can discover subtle bugs on data configurations that the developer wasn’t clever enough to think of (or energetic enough to set up). This is just a different way of saying that contract checks can take advantage of a MMTS.
  4. Leveraging Bugfixes – Most companies have a set of tests to ensure that fixes for customer-reported bugs don’t regress. The good news is that these test the code on realistic customer data. The bad news is that each test covers only the particular data configuration which failed. If these tests were instead formulated as contract checks (by inserting a failing contract which succeeds afterthe bugfix), then the new contract checks will be run on other data configurations in other tests. This leads to an incremental process where each fixed bug contributes non-linearly to the overall robustness of the product, both by adding a contract test to the code and by adding another data configuration upon which many contract checks will be executed.
  5. Defect Localization – one of the primary arguments for making unit tests small and simple is so that it is easy to isolate the functionality which is broken when the tests break. With contract checks, this is a non-issue – the debugger takes the developer to the method containing the first “test” failure. Stef talks about this point in the context of expressive tests that only test one thing.
  6. Encapsulation – if you think about it, the argument “unit tests should be small, so that it’s easy to isolate which method is broken” is a subtle code smell indicating that putting the validation code outside the methods is a violation of encapsulation (which contract checks solve). Contract checks also avoid the temptation to make private data public so that the test code can get at it; as part of the method, the contract check has access to private data.

This is why I got so excited when I read Stef’s post – the frustrations she expressed lined up with my thoughts about the benefits of including contract checks as part of a unit (or system) testing strategy.

Before I go, I want to make one thing clear: I am not saying that xUnit is the source of the problems I’ve discussed above; it’s simply a very useful tool that makes it easy to write and run automated tests. Nor am I saying that contract checks are a silver bullet that will remove all complexity from the test code so that the techniques in Meszaros’ book will be unnecessary. What I am saying is that contract checking is an essential part of any testing strategy, and that developers who don’t use a contract checking methodology run the risk of running into many of the anti-patterns that Meszaros points out (and addresses) throughout his book.

You might also like...

2 Min read
3D InterOp
Software developers in the manufacturing field are often tasked with implementing expert-level algorithms for 3D...
CNC Routing Software 1
7 Min read
3D InterOp
CNC routing software is an indispensable tool that gives manufacturers new levels of precision and speed in product...
Application Lifecycle Management Flow
4 Min read
CGM Modeler
When you hear the term, Application Lifecycle Management (ALM), you likely think about the process that a software...
8 Min read
CGM Modeler
What is Computer Aided Manufacturing The CAM Market Who Uses CAM Software? Trends in CAM What do CAM Software...
7 Min read
3D InterOp
Table of Contents Why industrial automation is important Advantages and Disadvantages of Industrial Automation The...
8 Min read
CGM Modeler
What do you do? What Exactly is FEM? What You Need to Know About Choosing a FEM Modeler FEM and Partial Differential...
5 Min read
CGM Modeler
Computational Fluid Dynamics (CFD) is a type of analysis that provides insight into solving complex problems, and...
2 Min read
CGM Modeler
WRL files are an extension of the Virtual Reality Modeling Language (VRML) format . VRML file types enable browser...
Voxel model example
3 Min read
CGM Modeler
Voxels are to 3D what pixels are to 2D. Firstly -- let’s examine what pixels actually are. Everything you see on your...
Point_cloud_torus
2 Min read
CGM Modeler
Point-cloud modeling is typically used in the process of 3D scanning objects. Rather than defining surfaces through...
Polygonal Modeling
2 Min read
CGM Modeler
Polygonal (or polyhedral) modeling is the most common type of modeling for video games and animation studios. This type...
BREP Model example
2 Min read
CGM Modeler
BRep modeling, or Boundary Representation modeling, is, in CAD applications, the most common type of modeling. BRep is...
4 Min read
3D InterOp
(Stratasys V650 Flex. Source: Javelin-Tech) 3D printing has emerged as a popular 3D model fabrication option. Be it...
CAD System Components
4 Min read
CGM Modeler
Effective computer-aided design (CAD) and computer-aided manufacturing (CAM) programs include the following main...
Voxeldance and Spatial
2 Min read
3D InterOp
To the uninitiated, 3D printing may seem a simple process — download your CAD file and hit print. But the world of...
29556
4 Min read
3D InterOp
Due to different workflows, it is not uncommon to find companies collaborating with one another (e.g. a...
dimension-image.jpg
2 Min read
3D InterOp
Recently I was asked a question about one of the standard test files generated by NIST (National Institute for...
4 Min read
3D InterOp
Part and parcel with model-based engineering is model translation. Because the model is now the specification, accurate...