code_junkie

JUnit test with dynamic number of tests

Nov 12th, 2011
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. @RunWith(Parameterized.class)
  2.  
  3. @Parameters
  4.  
  5. @RunWith(Parameterized.class)
  6. public class ParameterizedTest {
  7.  
  8. private File file;
  9.  
  10. public ParameterizedTest(File file) {
  11. this.file = file;
  12. }
  13.  
  14. @Test
  15. public void test1() throws Exception { }
  16.  
  17. @Test
  18. public void test2() throws Exception { }
  19.  
  20. @Parameters
  21. public static Collection<Object[]> data() {
  22. // load the files as you want
  23. Object[] fileArg1 = new Object[] { new File("path1") };
  24. Object[] fileArg2 = new Object[] { new File("path2") };
  25.  
  26. Collection<Object[]> data = new ArrayList<Object[]>();
  27. data.add(fileArg1);
  28. data.add(fileArg2);
  29. return data;
  30. }
  31. }
  32.  
  33. public class XTest extends TestCase {
  34.  
  35. public File file;
  36.  
  37. public XTest(File file) {
  38. super(file.toString());
  39. this.file = file;
  40. }
  41.  
  42. public void testX() {
  43. fail("Failed: " + file);
  44. }
  45.  
  46. }
  47.  
  48. public class XTestSuite extends TestSuite {
  49.  
  50. public static Test suite() {
  51. TestSuite suite = new TestSuite("XTestSuite");
  52. File[] files = new File(".").listFiles();
  53. for (File file : files) {
  54. suite.addTest(new XTest(file));
  55. }
  56. return suite;
  57. }
  58.  
  59. }
  60.  
  61. import org.junit.Test;
  62. import org.junit.runner.RunWith;
  63. import org.junit.runners.Parameterized;
  64. import org.junit.runners.Parameterized.Parameters;
  65.  
  66. @RunWith(Parameterized.class)
  67. public class TestY {
  68.  
  69. @Parameters
  70. public static Collection<Object[]> getFiles() {
  71. Collection<Object[]> params = new ArrayList<Object[]>();
  72. for (File f : new File(".").listFiles()) {
  73. Object[] arr = new Object[] { f };
  74. params.add(arr);
  75. }
  76. return params;
  77. }
  78.  
  79. private File file;
  80.  
  81. public TestY(File file) {
  82. this.file = file;
  83. }
  84.  
  85. @Test
  86. public void testY() {
  87. fail(file.toString());
  88. }
  89.  
  90. }
  91.  
  92. [RowTest]
  93. [Row( 1000, 7, 142.85715)]
  94. [Row( 1000, 0.00001, 100000000)]
  95. [Row(4195835, 3145729, 1.3338196)]
  96. public void DivisionTest(double numerator, double denominator, double result)
  97. {
  98. Assert.AreEqual(result, numerator / denominator, 0.00001);
  99. }
  100.  
  101. public void testAllFiles()
  102. {
  103. string sCollectingParam = "";
  104. // loop for each file
  105. // if test condition fails (can't use junit assert as that would bail out on first failure)
  106. // add to sCollectingParam with a descriptive log with filename
  107.  
  108. assertEquals("Some files failed the test", "", sCollectingParam);
  109. }
Add Comment
Please, Sign In to add comment