How do I pass parameters to a Junit test from Ant?

2020-07-06 02:42发布

问题:

I'm using Junit under Ant to perform Selenium Test.My test cases need to read files which contain test data(in order to accomplish data driven test). I don't mind embedding the file names in the test cases, but I'd like to have the name of the directory where the data files are stored parameterized in the build.xml file.

What's the best way to pass information like that from build.xml down into the test cases? Is it a good idea to use ant property? Is it possible to inject Junit4 parameter from build.xml?

回答1:

The junit task accepts nested sysproperty elements.

<junit fork="no">
  <sysproperty key="mydatadir" value="${whatever}"/>
  ...
</junit>

You can access these from within your tests using System.getProperty().