ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Lenz <cml...@gmx.de>
Subject Re: JUnit and loading external test data from file
Date Wed, 18 Jun 2003 15:44:42 GMT
W. Sean Hennessy wrote:
> You might consider decoupling your test code dependencies from Ant and
> bundle the testdatafile.txt in a testresoures.jar file.
> Better mechanisms might be found in a java.list.
> 
> getResourceAsStream("testdatafile.txt");

No need to create a JAR for that.
Simply copy the test resource files into the same directory into which the 
corresponding test classes get compiled:

   build/classes
     |-- mypackage
     |    |-- MyTest.class
     |    |-- MyTestInput.txt

Then using MyTest.class.getResourceAsStream("MyTestInput.txt") should work.

-chris

> -----Original Message-----
> From: Flemming Haurum [mailto:fha@lyngsoesystems.com]
> Sent: Wednesday, June 18, 2003 7:10 AM
> To: 'Ant Users List'
> Subject: RE: JUnit and loading external test data from file
> 
> 
> Yes I believe you are right if I have one directory with Junit code.
> But I have several directories and therefore I am using the
> batchtest+filetest and if I should use the sysproperty solution I would need
> to set it dynamically, or am I wrong?
> 
> It would be fine if I could use something like (pseudo code):
> 
>  new FileReader (JUNIT_TEST_DIR + "/testdatafile.txt");
> 
> But how to set the JUNIT_TEST_DIR dynamically for each instance found by the
> batchtest+fileset?
> 
> The test data files are located in the same directory as the Junit java
> source files.
> 
> /Flemming
> 
>  -----Original Message-----
> From: 	Nicolas Johnson [mailto:nicolas.johnson@e-markets.com]
> Sent:	18. juni 2003 15:49
> To:	Ant Users List
> Subject:	RE: JUnit and loading external test data from file
> 
> You might try an alternative solution.  When I use external files in my Test
> classes I get the file name from a property.   I set the property in a
> forked jvm instance that is used when running JUnit.  Something like:
> 
> <junit fork="yes" haltonfailure="yes">
>     <sysproperty key="test.path" value="${test_docs}"/>
> 
>     ...
> 
> </junit>
> 
> that way I can easily change the path to the test files without having to
> recompile the sourcecode.
> 
> -----Original Message-----
> From: Flemming Haurum [mailto:fha@lyngsoesystems.com]
> Sent: Wednesday, June 18, 2003 4:58 AM
> To: 'user@ant.apache.org'
> Subject: JUnit and loading external test data from file
> 
> 
> I have a problem with Junit test code that needs to load external test data
> from files.
> 
> My test code opens the test data file the following way:
> try {
> FileReader fileReader = new FileReader("testdatafile.txt");
> 
>             bufferedReader = new BufferedReader(fileReader);
> 
> 		.
> 		.
> 		.
>         } catch (FileNotFoundException e) {
>             assertTrue("Run the unit test from within the test
> directory!!!!" +
>                        e.getMessage(), false);
>         } catch (IOException e) {
>             assertTrue(e.getMessage(), false);
>         }
> 
> It works when I am running the Junit from the directory where the test code
> and the test files are located. ant -find build.xml junit
> But when I run the Junit from a higher level in the directory structure it
> fails as the test data file is specified by name without path. I would hate
> if I have to use absolute file reference as
> "/usr/fha/project/test/com/flemming/util/testdatafile.txt".
> 
> My build.xml contains the following regarding the junit.
> 
>     <junit printsummary="yes" haltonfailure="no">
>         <classpath refid="component.testClasspath"/>
> 
>         <formatter type="xml" />
> 
>         <!-- JUnit has its own class loader that cannot handle the multiple
> XML
>         classes found in our class path. Therefore it is necessary with a
>         seperate VM for the unit tests.  -->
>         <batchtest fork="yes" todir="${build.junitReport}">
>             <fileset dir="${build.testsrc}">
>                 <include name="**/*Test.java" />
>                 <exclude name="**/AllTests.java" />
>             </fileset>
>         </batchtest>
>     </junit>
> 
> Should I use the 'dir' attribute to specify that the VM should be invoked in
> a different directoy? And in that case how to write the statement as I am
> using the batchtest+fileset to find directories containing the Junit test
> code.
> 
> Do some of you have experience or advise on how to load the test data files.
> 
> Regards Flemming
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message