Unit Testing - pc2ccs/pc2v9 GitHub Wiki

Unit Testing is primarily done using JUnit tests. This article describes different ways to run the PC2v9 JUnit tests

Testing using build.xml

The Ant target named test can be used to run all JUnit tests. This is the unit testing that the CI-CD build-deploy process uses to unit test.

Directions for running JUnit test via build.xml in Eclipse:

  • Select the build.xml file.
  • Right click to bring up context menu.
  • Select Run As....
  • Select 2. Ant Build...
  • Check test, uncheck other targets/names.
  • Click Run.

All tests should pass, especially before pushing any changes.

There may be warnings similar to the following:

[junit] IOexception in setFileHandlers Couldn't get lock for profiles\Pa2427e06-a349-4f12-9ea2-ed1c579301bc\pc2.startup-%u.log

Such warnings can be ignored.

Testing using Eclipse

Preparation

When running JUnit tests under Eclipse, sometimes a large number of tests (20 or more) fail or there is a message like this:

junit.framework.AssertionFailedError: ERROR - pc2 jar path not a directory '/software/pc2/cc/projects/pc2v9/build/prod\'

This situation usually means that there is no pc2.jar file available for running JUnit tests. To create the pc2.jar for unit testing, do the following:

  • Select the createVERSIONandJar.xml file.
  • Right click.
  • Select Run As...
  • Select 1. Ant Build

This will run the createVERSIONandJar.xml file, which will create the missing pc2.jar file.

Executing the JUnit Tests

The following directions will behave like the build.xml test and may take more than 6 minutes to complete.

  • Select the test folder.
  • Right click.
  • Select Run As.
  • Select 1. Ant build.

This will run all (approximately 800+) JUnit tests. All tests should pass.

Faster Test

There is an option to run all the unit tests faster. The stress tests and longer running unit tests will not be run. Using this option may take a minute or less (but of course it skips numerous tests).

Use the same instructions as in Executing the JUnit Tests (above) except first edit the Run Configuration and add an environment variable named pc2fasttest and set its value to any.

This will speed up the JUnit testing because each JUnit has a conditional check such that if the pc2fasttest environment variable exists then that test will be skipped.

Note that while this is very useful during development, the FULL JUnit test suite (with the pc2fasttest variable removed/undefined) should always be run prior to pushing major updates.

Developer Notes and Tips

Extend AbstractTestCase class

The AbstractTestCase has a number of useful methods incuding:

public void assertFileExists(String filename, String message) 
public void assertDirectoryExists(String directoryName, String message) 
public void assertNotEquals(String message, String expected, String actual) 
public void assertEquals(String message, Filter expected, Filter actual) 
public void assertFirstValueFound(String xml, String tagName, String expectedValue) 

Use SampleContest

Contains methods to create model and add/modify model and utility methods

// Create "Standard" contest (pre-populated)
public IInternalContest createStandardContest() {
// Adding a run to the contest based on the runInfoLine, see example in EventFeedJSONTest see UnitTestData constructor
public static void addRunFromInfo(IInternalContest contest, String runInfoLine, boolean computerJudged) 

Loading a CDP into the model

Load the model from a CDP

// ex. to load from CDP at dirname
      File cdpConfigDirectory = new File(dirname);
      IInternalContest contest = new InternalContest();
      loader.initializeContest(contest, cdpConfigDirectory);

Load the model from a samps contest

// Example in ContestTest class
Use 
 private IInternalContest loadSampleContest(IInternalContest contest, String sampleName) throws Exception {
Ex.
     IInternalContest internal = loadSampleContest(null, "valtest");

See Also