Skip to main content

Example Driven Development and Unit testing

In a project I once worked, we were required by company standards to write formal test specifications for manual testing. It was a lot of overhead to write these Word documents and get them through the bureaucracy. Finally, we proposed to write the test specifications as comments inside functional unit tests. That way, we could maintain the test documents easily. We saved a lot of work, and the documents got higher quality because it was easier to update them. And it really helped us to write good unit tests that covered the functionality.

In this post, I will take this approach one step further to show how unit tests and manual tests can be unified.

In an earlier post Example driven development, I argued that a few simple examples can be used for requirements, manual testing and unit testing. I don't say that a few examples are sufficient as a requirements specification, but they may be in relatively simple projects. And it is far better than nothing. Examples help you to think clearly, and to communicate accurately with others. Don't we all use examples when we try to explain something? That's the best way to explain something anyway.

The problem is that it is hard to keep the examples up to date, and then they lose the value they had for communication with the client and manual testing.

But if we implement the examples as unit tests, we completely avoid this problem! As long as the unit tests pass, the examples will be in sync with the code. And if the client changes the requirements, we modify the unit tests, and then implement the changes until the unit tests pass.

Unit tests are hard to read for non-programmers, but if we put a lot of effort into it, we can make them readable. They don't need to be writable, as programmers will write them.

Here is an example of a test of a servlet that generates licenses:
   public void testGenerateLicense() throws Exception
   {
      // Call the servlet.
      InputParams params = new InputParams();
      params.productId = "123456";
      params.quantity = 1;
      params.firstName = "Lars";
      params.lastName = "Høidahl";
      params.email = "lars@mycompany.com";
      params.company = "Object Generation";
      params.country = "Sweden";
      File licenseFile = servlet.generateLicense(params);
      
      // Check the returned file.
      assertTrue("Attachment is a license file",
            licenseFile.getName().endsWith(".lic"));
      
      // Check that 1 user was created in the database.
      List users = userDatabase.getAllUsers();
      assertEquals("Users", 1, users.size());
      User user = users.get(users.size()-1);
      assertEquals("User name", "Lars", user.getUsername());
      assertEquals("Email", "lars@mycompany.com", user.getEmail());
      assertEquals("Country", "Sweden", user.getCountry());
   }

   public void testGenerateMultipleLicenses() throws Exception
   {
      // Call the servlet.
      InputParams params = new InputParams();
      params.productId = "123456";
      params.quantity = 3;
      params.firstName = "Lars";
      params.lastName = "Høidahl";
      params.email = "lars@mycompany.com";
      params.company = "Object Generation";
      params.country = "Sweden";
      File licenseFile = servlet.generateLicense(params);
      
      // Check the returned file.
      assertTrue("Attachment is a zip file",
            licenseFile.getName().endsWith(".zip"));
      ZipFile zipFile = new ZipFile(licenseFile);
      assertEquals("Number of entries", 3, zipFile.size());
      
      // Check that 3 users were created in the database.
      List users = userDatabase.getAllUsers();
      assertEquals("Users", 3, users.size());
      User user = users.get(users.size()-1);
      assertEquals("User name", "Lars", user.getUsername());
      assertEquals("Email", "lars@mycompany.com", user.getEmail());
      assertEquals("Country", "Sweden", user.getCountry());
   }

Comments

Popular posts from this blog

Database dump with Java

I need to update a database that is created by PHP. The problem is that I am not a PHP coder, but a Java coder, and I need to use some other Java libraries to get the job done. So how can find out exactly which tables to update and how? It would take me weeks to search the PHP code, and I still wouldn't be sure if I got it right.

The first step is to install a clean application on my computer. There is no user data in the database, so if I perform commands like creating a user etc in the web application, I can look at what changed in the database. I'm sure that could be done in MySQL, but I'm not an expert on that either. When the only tool you have is a hammer, everything looks like a nail. So, I'll use Java for that to.

So, I wrote a small Java application that produces exactly the output that I need. It reads metadata from the database to find all tables and columns, lists that metadata and the content of all the rows.

Here it is:import java.io.FileNotFoundException;
im…

The Future of Programming

The problem with abstractions Programmers are experts in abstract thinking. To program is to generalize: A method is a general specification of different execution paths. A class is a general specification of objects. A superclass is a generalization of several classes.

Although our minds are capable of abstract thinking, concrete thinking is more natural, and concrete examples are the foundation for abstractions. For instance, how do you teach children what a car is? You don't give a definition like: 'A car is a wheeled motor vehicle used for transporting passengers.' Instead, you point at a car and say: 'Look, a car!' After seeing a few cars, the child understands what a car is.


Notice what I just did! I started with a general principle and then gave an example. Which part of the paragraph above is easier to understand, the first sentence (general) or the rest (example)?

Einstein said that examples is not another way to teach, it is the only way to teach. It is…

The Pessimistic Programmer

I decided to change the title of this blog to "The Pessimistic Programmer". Why? Am I a depressed person that thinks nothing will work? No, I am an optimist in life. Something good is going to happen today :-) But in programming, something will surely go wrong.

I don't actually view this as pessimism, but as realism. I want to be prepared for the worst that can possibly happen. Hope for the best, prepare for the worst. But my wife explained to me that pessimists always say that they are just being realistic. So, I might as well face it: I am a pessimist.

I think a good programmer needs to be pessimistic; always thinking about what can go wrong and how to prevent it. I don't say that I am a good programmer myself. No, I make far too many mistakes for that. But I have learnt how to manage my mistakes with testing and double checking.

Über-programmers can manage well without being pessimistic. They have total overview of the code and all consequences of changes. But I'…