Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a unit test framework?

  • How to write a unit test framework?
  • Can anyone suggest some good reading?

I wish to work on basic building blocks that we use as programmers, so I am thinking of working on developing a unit test framework for Java. I don't intend to write a framework that will replace junit; my intention is to gain some experience by doing a worthy project.

like image 376
user892871 Avatar asked Aug 13 '11 08:08

user892871


People also ask

What is a unit testing framework?

What are unit test frameworks and how are they used? Simply stated, they are software tools to support writing and running unit tests, including a foundation on which to build tests and the functionality to execute the tests and report their results.

How do you structure a unit test?

A typical unit test contains 3 phases: First, it initializes a small piece of an application it wants to test (also known as the system under test, or SUT), then it applies some stimulus to the system under test (usually by calling a method on it), and finally, it observes the resulting behavior.

How do I choose a unit test framework?

Which are the most important areas to test? Determine what you want to focus on before jumping into testing. Identify the highest-priority application features or areas that must be tested. Identify a list of scenarios within each feature to test, and to consider which level of testing can fulfill each.


3 Answers

There are several books that describe how to build a unit test framework. One of those is Test-Driven Development: By Example (TDD) by Kent Beck. Another book you might look at is xUnit Test Patterns: Refactoring Test Code by Gerard Meszaros.

  • Why do you want to build your own unit test framework?
  • Which ones have you tried and what did you find that was missing?

If (as your comments suggest) your objective is to learn about the factors that go into making a good unit test framework by doing it yourself, then chapters 18-24 (Part II: The xUnit Example) of the TDD book show how it can be done in Python. Adapting that to Java would probably teach you quite a lot about Python, unit testing frameworks and possibly Java too.

It will still be valuable to you to have some experience with some unit test framework so that you can compare what you produce with what others have produced. Who knows, you might have some fundamental insight that they've missed and you may improve things for everyone. (It isn't very likely, I'm sorry to say, but it is possible.)

Note that the TDD people are quite adamant that TDD does not work well with databases. That is a nuisance to me as my work is centred on DBMS development; it means I have to adapt the techniques usually espoused in the literature to accommodate the realities of 'testing whether the DBMS works does mean testing against a DBMS'. I believe that the primary reason for their concern is that setting up a database to a known state takes time, and therefore makes testing slower. I can understand that concern - it is a practical problem.

like image 103
Jonathan Leffler Avatar answered Oct 13 '22 00:10

Jonathan Leffler


Basically, it consists of three parts:

  1. preparing set of tests
  2. running tests
  3. making reports

Preparing set of tests means that your framework should collect all tests which you want to run. You can specify these tests (usually classes with test methods which satisfy some convention or marked with certain annotation or implement marker interface) in a separate file (java or xml), or you can find them dynamically (making a search over classpath).

If you choose the dynamic searching, then you'll probably have to use some libraries which can analyse java bytecode. Otherwise you'll have to load all the classes in your classpath, and this a) requires much time and b) will execute all static initializers of loaded classes and can cause unexpected tests results.

Running tests can vary significantly depending on features of your framework. The simplest way is just calling test methods inside a try/catch block, analysing and saving results (you have to analyze 2 situations - when the assertion exception was thrown and when it was not thrown).

Making reports is all about printing analyzed results in xml/html/wiki or whatever else format.

like image 25
Roman Avatar answered Oct 13 '22 00:10

Roman


The Cook's Tour is written by Kent Beck (I believe; it's not attributed), and describes the thought process that went into writing JUnit. I would suggest reading it and considering how you might choose an alternate line of development.

like image 32
parsifal Avatar answered Oct 13 '22 00:10

parsifal