JUnit
From Informatics
Java Unit Test framework.
Contents |
Simple Tutorial
Unit Testing in Eclipse Using JUnit
Unit test in geWorkbench
Where
We put our unit tests under "test" directory.
Naming System
File Names
XxxTest.java
We name each unit test according to the file name we want to test.
If the file we want to test is Xxx.java in component Aaa, the unit tests will be in XxxTest.java under Aaa/test directory.
ex: the unit test file for AnovaAnalysis.java is named "AnovaAnalysisTest.java" under "test" directory in "anova" component.
XxxGuiTest.java
We separate GUI test from non-GUI test (We plan to run unit test before each build on server, which use headless device, means no monitor for graphic interface).
If the file we want to test is Xxx.java in component Aaa, the unit tests for methods contains GUI output will be in XxxGuiTest.java under Aaa/test directory.
ex: the unit test file for ProjectPanel.java, which contains GUI interaction, is named "ProjectPanelGuiTest.java" under "test" directory in "geworkbench".
Method Names
Eclipse can automatically generate names for all the testing methods, it will start with a prefix "test" and postfix with arguments if multiple methods use different arguments but same method names.
ex: if you want to test a method called "execute" the method name for testing method will be "testExecute"
You can check method names for all public methods in AnovaAnalysis.java, and compare with AnovaAnalysisTest.java. Method names in AnovaAnalysisTest.java are all generated by eclipse.
Examples
implementing basic unit test
Please see AnovaAnalysisTest.java
This example demonstrate how to write unit test, and it's naming system.
It basically test if each method working properly or not, by giving input and check for expected output.
implementing unit test using data in System Test
Please see HierarchicalClusteringTest.java
This provides example code usage for following programmers who need to reuse the code.
implementing unit test which check for GUI output
There's several ways to do this,
- control the GUI using java, ex: call Button1.onClick() to trigger the method when user click the button, and test if TestField1.text == expect result.
- take a snapshot of expect result, save in "test" directory, next time, when doing unit test, just take another snapshot, and compare with the image saved under "test" directory.
How to generate JUnitReport for components
1. put unit test files under "test\" folder
ex: components\anova\test\org\geworkbench\components\anova
and give it a name end by Test.java
ex: AnovaAnalysisTest.java
2. use "ant junit-run" to generate junit report for geworkbench and all components.
3. the report will be under "testing\junitreport" folder
ex: components\anova\ components\anova\testing\junitreport