Skip to content

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

← Back to blog | ADMIN | 01-02-2011

My instinct is that there’s something wrong if one needs to spend a large fraction of ones architectural effort on code which is not part of the shipped product

Hexagon pattern 1

In her post that started this thread, Stef mentioned Meszaros' book on xUnit.  My reaction when I bought the book was a bit different from hers; it was what I would now describe as a smell.  My instinct was that there’s something wrong if one needs to spend a large fraction of one’s architectural effort on code which is not part of the shipped product.

 

Note that I am not saying that effort spent on testing and test automation is valueless if the tests aren’t part of the shipped product.  Spatial has been running hundreds of thousands of (scripted) ACIS tests every night since before I joined the company oh so many years ago; our test suite is one of our most valuable pieces of IP.  Instead, my instinct was that that typical usage of what Meszaros calls the "Four-Phase Testpattern probably violates some core OO design principle, which in turn requires expending a lot of (architectural) cleverness to manage the resulting complexity. 

 

For reference, let me explicitly write down my (simplified) version of the four phases:

1.      Set up the input data (objects) and owning object(s) for the method(s) to be tested.

2.      Execute the method(s).

3.      Perform validation tests (usually a set of assertions) on the outputs (including the owning object(s)) to check the result.

4.      Return the xUnit application to its (hopefully) original “clean” state.

 

My original thought was that construction of data objects during setup (step 1) violates data hiding, which leads to tight coupling between the code and the tests.  Over the years, however, I’ve realized that there’s a much deeper issue with the steps above - you can probably guess what it is from the title of this post. J  Here’s my opinion:

 

The  xUnit tools often foster a testing methodology which violates a fundamental tenet of XP/Agile programming - Once and Only Once. 

 

This is because xUnit encourages developers to put validation code in the test (step 3), i.e. outside the method being tested.  This is turn results in either duplication of the validation code in multiple tests, or architectural complexity in the test component to ensure that the same validation code is called from multiple tests.  Obviously, I think the solution to this dilemma lies in the introduction of contract checks, but that will have to wait until Part II....

 

 

You might also like...

5 Min read
3D Modeling
What is digital manufacturing? Here’s a simple digital manufacturing definition: the process of using computer systems...
5 Min read
3D Modeling
Software components are like the stage crew at a big concert performance: the audience doesn’t see them, but their...
Application Lifecycle Management Flow
4 Min read
3D Modeling
When you hear the term, Application Lifecycle Management (ALM), you likely think about the process that a software...
8 Min read
3D Modeling
What is Computer Aided Manufacturing The CAM Market Who Uses CAM Software? Trends in CAM What do CAM Software...
9 Min read
3D Modeling
SLS in Additive Manufacturing is used to convert 3D CAD designs into physical parts, in a matter of hours.
8 Min read
3D Modeling
There’s a lot of confusion around what the terms additive manufacturing and 3D printing mean.
4 Min read
3D Modeling
Additive manufacturing, often referred to as 3D printing, is a computer-controlled process for creating 3D objects.
5 Min read
3D Modeling
Take a fresh, new sheet of paper, and fold it in half, like you’re making a paper airplane. Place the folded paper on...
6 Min read
3D Modeling
Table of Contents Simulation in CAD Who Uses Simulation Modeling? Key Benefits of Simulation Modeling Challenges in...
8 Min read
3D Modeling
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
3D Modeling
Computational Fluid Dynamics (CFD) is a type of analysis that provides insight into solving complex problems, and...
2 Min read
3D Modeling
WRL files are an extension of the Virtual Reality Modeling Language (VRML) format . VRML file types enable browser...
Voxel model example
3 Min read
3D Modeling
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
3D Modeling
Point-cloud modeling is typically used in the process of 3D scanning objects. Rather than defining surfaces through...
Polygonal Modeling
2 Min read
3D Modeling
Polygonal (or polyhedral) modeling is the most common type of modeling for video games and animation studios. This type...
aerodynamics-CFD
9 Min read
3D Modeling
Computational fluid dynamics (CFD) is a science that uses data structures to solve issues of fluid flow -- like...
BREP Model example
2 Min read
3D Modeling
BRep modeling, or Boundary Representation modeling, is, in CAD applications, the most common type of modeling. BRep is...
Feature Recognition Zoomed
5 Min read
3D Modeling
IN THIS ARTICLE: What is FEA (Finite Element Analysis) Principles of Finite Element Analysis and Simulation Software A...