Skip to content

UnitTesting

Razvan Deaconescu edited this page Jun 7, 2019 · 4 revisions

Unit Testing

Unit testing is the basis for TDD (Test Driven Development) where a developer designs tests before creating the actual program. Unit tests aim to validate a singular aspect of a program, and tipically you would end up with many of them; usually you will have more lines of tests than the actual program.

An important rule in unit testing is for each test to test a single feature of the program, not more.

Unit testing is not exhaustive. You should also consider integration testing and system testing. Also unit testing can be incorporated into regression testing and be the basics for continuous integration.

See this page for advice on doing unit testing.

For Java we use JUnit. See this page for pointers on using JUnit.

Concepts

defensive programming

testing, test driven development

unit testing, advantages and disadvantages

Tasks

Unit Test Example

Enter the 09-unit-testing/array/ subfolder in the repository. Check the source code file ArrayShiftTest.java.

Based on OOP Lab 5 from inf.ed.ac.uk.

Download the JUnit jar file from here. Compile the source code file using:

javac -cp .:junit-4.13-beta-3.jar ArrayShiftTest.java

We need properly fill the ArrayTest.java source code file that defines the class under test properly; we use TDD so we first define the interface and the tests and then we implement. Fill the TODO in ArrayTest.java. After properly creating the ArrayTest class recompile using the above command.

To test it run:

java -cp .:junit-4.13-beta-3.jar junit.textui.TestRunner ArrayShiftTest

Add an assertEquals() call to the testShiftOne() method that simply checks the output length.

Update Test Example

Move the call to as.shiftOne(inArray) in the setUp() function. Now create test methods with the following names:

  • testShiftFirst()
  • testShiftLength()
  • testShiftElements()

Even More

Enter the 09-unit-testing/array-mult/ subfolder in the repository. Check the source code files.

Fill the TODO items in ArrayMultTest.java. Then use them to test the program.

Unit Test Exercise

Do the exercise here.

Clone this wiki locally