|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
Help on Junit4-writing a parameterized test caseI am a beginner in Junit4. I need your help to write a parameterised
Junit test case. This will help me in learning Junit in a better way. I am trying to do data-driven testing in Junit4. I am reading data from CSV file and using it to run the test case. The code works well if all the test passes. If one test data fails, then the assert fails and it comes out of the method and remaining data is not tested. Can you provide me a code as to how should I write a parameterized test case for one method say intAdd() in the Example1.java file. How should I a parameterised Junit test case for a simple add method in class?? Example1.java that adds two integers. But the value of the two integers should be taken/used from the csv file. The Csv file is kept at path C:\TestData.csv. Following is code in my Example1.java class. import java.util.Date; public class Example1 { int intAdd(int c, int d) { int a = c + d; return a; } } The CSV file looks like this: Method name Value1 Value2 intAdd 5 6 intAdd 8 7 . . . . . . intAdd n values n values My testclass code is - package com; import static org.junit.Assert.*; import java.io.FileReader; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.experimental.theories.ParametersSuppliedBy; import au.com.bytecode.opencsv.CSVReader; import com.testdata.Example1; //@ParametersSuppliedBy public class Example1Test { private static final String ADDRESS_FILE = "C:/MyProject/com/test/TestData.csv"; @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { } @Test public void IntAdd() throws Exception{ Example1 ex = new Example1(); //assertEquals(9, ex.intAdd(4, 5)); CSVReader reader = new CSVReader(new FileReader(ADDRESS_FILE)); String [] nextLine; while ((nextLine = reader.readNext()) != null) { if("intAdd".equalsIgnoreCase(nextLine[0])){ assertEquals(new Integer(nextLine[3]).intValue(), ex.intAdd(new Integer(nextLine[1]).intValue(), new Integer(nextLine[2]).intValue())); } } } //@Test /*public void IntAdd2() { Example1 ex = new Example1(); assertEquals(9, ex.intAdd(4, 5)); }*/ Instead of passing the hardcoded values as 2 and 3, I want to write the class such that the Example1Test class will read the Data (method name, value1 and value2 )from my csv file and make it available to the assertEquals method. Also I want to iterate over the n values in csv file. Please can you help me how to write a parameterised Junit test case. I will be thankful to all. You can mail me the at madhufuge@... Madhavi [Non-text portions of this message have been removed] |
|
|
Re: Help on Junit4-writing a parameterized test caseMadhavi,
The included code was badly mangled in transit. Can you try again? Thanks, David Saff On Wed, Oct 28, 2009 at 10:36 PM, madhufuge <madhufuge@...> wrote: > I am a beginner in Junit4. I need your help to write a parameterised > Junit test case. This will help me in learning Junit in a better way. I > am trying to do data-driven testing in Junit4. I am reading data from > CSV file and using it to run the test case. The code works well if all > the test passes. If one test data fails, then the assert fails and it > comes out of the method and remaining data is not tested. Can you > provide me a code as to how should I write a parameterized test case for > one method say intAdd() in the Example1.java file. How should I a > parameterised Junit test case for a simple add method in class?? > Example1.java that adds two integers. But the value of the two integers > should be taken/used from the csv file. The Csv file is kept at path > C:\TestData.csv. Following is code in my Example1.java class. import > java.util.Date; public class Example1 { int intAdd(int c, int d) > { int a = c + d; return a; } } The > CSV file looks like this: Method name Value1 > Value2 intAdd 5 6 intAdd > 8 7 . . > . . . . intAdd > n values n values My testclass code is - package com; import > static org.junit.Assert.*; import java.io.FileReader; import > org.junit.After; import org.junit.Before; import org.junit.Test; import > org.junit.experimental.theories.ParametersSuppliedBy; import > au.com.bytecode.opencsv.CSVReader; import com.testdata.Example1; > //@ParametersSuppliedBy public class Example1Test { private static > final String ADDRESS_FILE = "C:/MyProject/com/test/TestData.csv"; > @Before public void setUp() throws Exception { } @After public void > tearDown() throws Exception { } @Test public void IntAdd() throws > Exception{ Example1 ex = new Example1(); //assertEquals(9, > ex.intAdd(4, 5)); CSVReader reader = new CSVReader(new > FileReader(ADDRESS_FILE)); String [] nextLine; while ((nextLine = > reader.readNext()) != null) { > if("intAdd".equalsIgnoreCase(nextLine[0])){ assertEquals(new > Integer(nextLine[3]).intValue(), ex.intAdd(new > Integer(nextLine[1]).intValue(), new Integer(nextLine[2]).intValue())); > } } } //@Test /*public void IntAdd2() { Example1 ex = new > Example1(); assertEquals(9, ex.intAdd(4, 5)); }*/ Instead of > passing the hardcoded values as 2 and 3, I want to write the class such > that the Example1Test class will read the Data (method name, value1 and > value2 )from my csv file and make it available to the assertEquals > method. Also I want to iterate over the n values in csv file. Please > can you help me how to write a parameterised Junit test case. I will be > thankful to all. You can mail me the at madhufuge@... Madhavi > > > [Non-text portions of this message have been removed] > > > > ------------------------------------ > > Yahoo! Groups Links > > > > |
|
|
Re: Help on Junit4-writing a parameterized test caseHello,
to help you in your problem, here are some helpful links: How to write parameterized tests <http://binkley.blogspot.com/2007/03/writing-junit-4-parameterized-tests\ .html> A nice PDF presentation from Wakaleo (also covers Parameterized tests) <http://www.wakaleo.com/public_resources/wjug-junit4.pdf> JUnit API page for Parameterized runner <http://junit.sourceforge.net/javadoc_40/org/junit/runners/Parameterized\ .html> You also said that you need to let the test run to the finish, and not stop on the first failure. This means that in your test method you have to just record a failure, and then move on (perhaps by catching the assertion error, and recording it). This way all your data will be tested, and in the end you can get a report with all the data points that caused the test to fail. For this you will probably need a Rule. Release notes covering all the available Rules <http://github.com/KentBeck/junit/blob/master/doc/ReleaseNotes4.7.txt> . I think you will need the TestWatchman and/or the ErrorCollector. Regards, Bogdan Mocanu --- In junit@..., "madhufuge" <madhufuge@...> wrote: > > I am a beginner in Junit4. I need your help to write a parameterised > Junit test case. This will help me in learning Junit in a better way. I > am trying to do data-driven testing in Junit4. I am reading data from > CSV file and using it to run the test case. The code works well if all > the test passes. If one test data fails, then the assert fails and it > comes out of the method and remaining data is not tested. Can you > provide me a code as to how should I write a parameterized test case for > one method say intAdd() in the Example1.java file. How should I a > parameterised Junit test case for a simple add method in class?? > Example1.java that adds two integers. But the value of the two integers > should be taken/used from the csv file. The Csv file is kept at path > C:\TestData.csv. Following is code in my Example1.java class. import > java.util.Date; public class Example1 { int intAdd(int c, int d) > { int a = c + d; return a; } } The > CSV file looks like this: Method name Value1 > Value2 intAdd 5 6 intAdd > 8 7 . . > . . . . intAdd > n values n values My testclass code is - package com; import > static org.junit.Assert.*; import java.io.FileReader; import > org.junit.After; import org.junit.Before; import org.junit.Test; import > org.junit.experimental.theories.ParametersSuppliedBy; import > au.com.bytecode.opencsv.CSVReader; import com.testdata.Example1; > //@ParametersSuppliedBy public class Example1Test { private static > final String ADDRESS_FILE = "C:/MyProject/com/test/TestData.csv"; > @Before public void setUp() throws Exception { } @After public void > tearDown() throws Exception { } @Test public void IntAdd() throws > Exception{ Example1 ex = new Example1(); //assertEquals(9, > ex.intAdd(4, 5)); CSVReader reader = new CSVReader(new > FileReader(ADDRESS_FILE)); String [] nextLine; while ((nextLine = > reader.readNext()) != null) { > if("intAdd".equalsIgnoreCase(nextLine[0])){ assertEquals(new > Integer(nextLine[3]).intValue(), ex.intAdd(new > Integer(nextLine[1]).intValue(), new Integer(nextLine[2]).intValue())); > } } } //@Test /*public void IntAdd2() { Example1 ex = new > Example1(); assertEquals(9, ex.intAdd(4, 5)); }*/ Instead of > passing the hardcoded values as 2 and 3, I want to write the class such > that the Example1Test class will read the Data (method name, value1 and > value2 )from my csv file and make it available to the assertEquals > method. Also I want to iterate over the n values in csv file. Please > can you help me how to write a parameterised Junit test case. I will be > thankful to all. You can mail me the at madhufuge@... Madhavi > > > [Non-text portions of this message have been removed] > [Non-text portions of this message have been removed] |
|
|
Re: Help on Junit4-writing a parameterized test caseHi!
Instead of using Rules (or do I missunderstand something?), you could annotate the test-class MyTest with @Runwith(Parametrized.class) and then a public static method with @Parameters returning a List<Object[]> In your case, create, maybe an ArrayList<Object[]>, to which you add for each row in your csv file a new Object[]-instance (maybe containing 3 Integers). Then, you need a public contructor MyText(Integer a, Integer b, Integer c) which assigns the parameters to fields (aka the fixture). A (set of) methods annotated with @Test can then run tests for these values. Each row in your csv file will be run against each @Test-annotated method (failure or not occuring). Cheers, Georg --- In junit@..., "Bogdan" <bogdan.mocanu.notifications@...> wrote: > > Hello, > > to help you in your problem, here are some helpful links: > > How to write parameterized tests > <http://binkley.blogspot.com/2007/03/writing-junit-4-parameterized-tests\ > .html> > > A nice PDF presentation from Wakaleo (also covers Parameterized tests) > <http://www.wakaleo.com/public_resources/wjug-junit4.pdf> > > JUnit API page for Parameterized runner > <http://junit.sourceforge.net/javadoc_40/org/junit/runners/Parameterized\ > .html> > > You also said that you need to let the test run to the finish, and not > stop on the first failure. This means that in your test method you have > to just record a failure, and then move on (perhaps by catching the > assertion error, and recording it). This way all your data will be > tested, and in the end you can get a report with all the data points > that caused the test to fail. > > For this you will probably need a Rule. Release notes covering all the > available Rules > <http://github.com/KentBeck/junit/blob/master/doc/ReleaseNotes4.7.txt> > . I think you will need the TestWatchman and/or the ErrorCollector. > > Regards, > Bogdan Mocanu > > > --- In junit@..., "madhufuge" <madhufuge@> wrote: > > > > I am a beginner in Junit4. I need your help to write a parameterised > > Junit test case. This will help me in learning Junit in a better way. > I > > am trying to do data-driven testing in Junit4. I am reading data from > > CSV file and using it to run the test case. The code works well if all > > the test passes. If one test data fails, then the assert fails and it > > comes out of the method and remaining data is not tested. Can you > > provide me a code as to how should I write a parameterized test case > for > > one method say intAdd() in the Example1.java file. How should I a > > parameterised Junit test case for a simple add method in class?? > > Example1.java that adds two integers. But the value of the two > integers > > should be taken/used from the csv file. The Csv file is kept at path > > C:\TestData.csv. Following is code in my Example1.java class. import > > java.util.Date; public class Example1 { int intAdd(int c, int > d) > > { int a = c + d; return a; } } The > > CSV file looks like this: Method name Value1 > > Value2 intAdd 5 6 intAdd > > 8 7 . . > > . . . . intAdd > > n values n values My testclass code is - package com; > import > > static org.junit.Assert.*; import java.io.FileReader; import > > org.junit.After; import org.junit.Before; import org.junit.Test; > import > > org.junit.experimental.theories.ParametersSuppliedBy; import > > au.com.bytecode.opencsv.CSVReader; import com.testdata.Example1; > > //@ParametersSuppliedBy public class Example1Test { private static > > final String ADDRESS_FILE = "C:/MyProject/com/test/TestData.csv"; > > @Before public void setUp() throws Exception { } @After public > void > > tearDown() throws Exception { } @Test public void IntAdd() throws > > Exception{ Example1 ex = new Example1(); //assertEquals(9, > > ex.intAdd(4, 5)); CSVReader reader = new CSVReader(new > > FileReader(ADDRESS_FILE)); String [] nextLine; while ((nextLine = > > reader.readNext()) != null) { > > if("intAdd".equalsIgnoreCase(nextLine[0])){ assertEquals(new > > Integer(nextLine[3]).intValue(), ex.intAdd(new > > Integer(nextLine[1]).intValue(), new > Integer(nextLine[2]).intValue())); > > } } } //@Test /*public void IntAdd2() { Example1 ex = new > > Example1(); assertEquals(9, ex.intAdd(4, 5)); }*/ Instead of > > passing the hardcoded values as 2 and 3, I want to write the class > such > > that the Example1Test class will read the Data (method name, value1 > and > > value2 )from my csv file and make it available to the assertEquals > > method. Also I want to iterate over the n values in csv file. > Please > > can you help me how to write a parameterised Junit test case. I will > be > > thankful to all. You can mail me the at madhufuge@ Madhavi > > > > > > [Non-text portions of this message have been removed] > > > > > > [Non-text portions of this message have been removed] > |
| Free embeddable forum powered by Nabble | Forum Help |