This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Testing and the fly likes Junit with XML inputs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Junit with XML inputs" Watch "Junit with XML inputs" New topic
Author

Junit with XML inputs

Syed Mahdi
Ranch Hand

Joined: Aug 27, 2004
Posts: 45
hello all,

I want to use JUnit, but instead of taking parametrized inputs from within a class, is it possible to have XMLs define the inputs so we give like 4 XMLs to a testcase and the same junit testcase is run four times iteratively with these four XMLs. Is there an add on for JUnit for that.

the only option after reading up a bit on JUNIT comes to me as making an XML parser and to marshall it to a testcase-parameter bean object which will represent a testcase scenario with defined parameters from the XMLs and I invoke the testcase with that four times.

is there a better or quicker way to do this.

Thanks in advance,

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30123
    
150

That sounds like the @Parameterized test case pattern. You can write code to read the XML files from there.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Syed Mahdi
Ranch Hand

Joined: Aug 27, 2004
Posts: 45
See this is what I said, for business analysts they cant use the paramterized pattern and code the testcases in Java. but they can make it in XML format. If I am using the paramterized pattern then I still need it to code and then parse the xml using dom or sax. You see I am looking at TestNG and it takes XMl as inputs. I was expecting the Junit must have by now added a feature where you specify the folder of the xml or even the xml and it runs the test case listed in it.

may be I didnt understand your answer, care to elaborate on the use of @Parameterized test case pattern

Thanks
mahdi
Brian Burress
Ranch Hand

Joined: Jun 30, 2003
Posts: 122
Syed Mahdi wrote:See this is what I said, for business analysts they cant use the paramterized pattern and code the testcases in Java. but they can make it in XML format. If I am using the paramterized pattern then I still need it to code and then parse the xml using dom or sax. You see I am looking at TestNG and it takes XMl as inputs. I was expecting the Junit must have by now added a feature where you specify the folder of the xml or even the xml and it runs the test case listed in it.


You provided a little more detail in your second post which helps define a little deeper what you are trying to do (i.e. the first post did not reveal that you are looking for a non coder to essentially write the tests). Are there other aspects of what you are trying to accomplish which you can lay out to help everyone have a better picture of the problem you are trying to solve?

Also, can you elaborate a little but on what it is you envision a BA coding in XML to have run in JUnit? It almost sounds like you are looking to have some sort of script interpreter get data from a file (XML in this case) and then figure out what to test. Are you trying to literally run the same tests (i.e. asserts) but with variable input data? If so, you may be able to consider a solution where the actual tests with asserts are coded in a helper method and then possibly a reference to the file is passed in, input read, and tests executed. The "testXXX" method could either be coded file by file, or itself could read in a list of the files to process (read in files in a definition list, read all files in a directory, etc).

Psuedo Code:

TestCase/TestSuite class
.
.
.
private void someTestSet(File parms)
{
get data from file
perform logic
asserts based on logic results
perform more logic
asserts based on logic results
etc.
}

public void testMyProject() throws Exception
{
instantiate objects for file with parms
someTestSet(parms)

- or-

get list of files
iterate through files
instantiate objects for file with parms
someTestSet(parms)

}


I may be coming in a little off base, but to test using JUnit then somewhere I would think you'll want to perform logic and have asserts which I would associate with Java coding. If you are looking to have the java code dynamically generated based on XML input or using something like reflection to execute code and then inspect results, then to your original post JUnit may not really be the right tool for what you want to accomplish.

Syed Mahdi
Ranch Hand

Joined: Aug 27, 2004
Posts: 45
Thanks for such a detailed reply, and sorry for replying so late.

I moved onto TestNG and actually found a good tool to do what I wanted: there is a tool called feeder4Junit and feeder4TetsNg and they are awesome tools for both testing frameworks. tho the junit one is a bit more mature than the testng version.

it takes a csv, excel file and gets the data from there the only problem is that the test method should have the same amount of parameters as the columns in the excel. I am testing through this and it is good.

thanks again for the reply and hope this helps someone as well.

Syed..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Junit with XML inputs
 
Similar Threads
Passing Parameter to Junit Test Case
testing with junit using xml
Ant for Junit Test cases using annotation.
GroovyTestCase
XML Parsing