• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Junit with XML inputs

 
Syed Mahdi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34973
378
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds like the @Parameterized test case pattern. You can write code to read the XML files from there.
 
Syed Mahdi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic