22.03.07 - Unit Testing & TDD
Unit Testing
White Block testing
- You know the code is supposed to work
- You tests test for how it is supposed to work
Black Box testing
- Where tester doesn't know how the code works
- Just know what is supposed to output for an input
- For release testing and user acceptance testing
Automated
- Large amounts of scripts or coded tests
- May automate large volume data testing
- Run as often the code is complied
- Built into control platform
Manual
- Higher level
- Usually run at planned times - end of key stages
- Can act as a progress gate-keeping activity
- Usually run on version of code that is 'ready' to test
Test Driven Development
- Take spec and start writing tests for a class
- Auto tests every time run code
- Whitebox testing
Advantages
- Integrates aspects of Specification, Coding and Testing
- Makes you think how code is used before being built
- Means you plan and get the bigger picture
- Also makes you know where the code failed based on the tests
JUnit
- Framework for writing tests for java code
- Its added to the project
- Has special commands to make testing easier
Can write @BeforeAll{}
functions. This does something before the test, like create text files for the test
Also have @AfterAll
to delete those text files
Can use this to create bad and good data
Also have @BeforeEach
and @AfterEach
Other Asserts
void assertTrue(boolean condition)
- Check that a condition is true.
void assertFalse(boolean condition)
- Check that a condition is false e.g. assertFalse(x > 12)
void assertNotNull(Object object)
- Check that an object isn't null.
void assertNull(Object object)
- Check that an object is null
void assertSame(boolean condition)
- Check that two objects are the
same (when equals wont work in java)
void assertNotSame(boolean condition)
- Check they are not
Good & Bad Tests
Write the tests first. Need to run the test to make sure it fails
Good Tests
- Line Coverage: Has every line of code been tested
- Function Coverage - Has every function been tested
- Condition Coverage - Has each evaluation point been tested
- Path Coverage - Has every possible route through the code been tested
- Entry/Exit coverage - has every call/return option been tested
- Should never have to edit a test