title: Should similar jUnit test cases reside within the same test?
body: <p>Okay, so I have a coursework problem sheet I've just finished, I know it works fine but I wanted to write some jUnit tests for it just as some testing practice. It sparked some questions. I wrote a function called <code>count</code> which takes in an <code>int n</code> and sums up all values from <code>1 to n</code> and returns it. So, I wrote 4 test cases for this and assumed they should all go within the same test method as such:</p>
  
  <pre><code>@Test
      public void testCount() {
          assertEquals(0, SimmonsPS5.count(0)); // 1 obj in table with name =&gt; 0 permutations
          assertEquals(1, SimmonsPS5.count(1)); // 2 obj in table with name =&gt; 1 permutation
          assertEquals(3, SimmonsPS5.count(2)); // 2 obj in table with name =&gt; 3 permutations (3c2)
          assertEquals(5050, SimmonsPS5.count(100)); // 100 obj in table with name =&gt; 100c2 permutations (100(100+1)/2)
      }
  </code></pre>
  
  <p>But then if one fails the test (which will probably mean they all do) the error reporter will only say that one test failed, which is not correct.</p>
  
  <p>Next, my main method in the class instantiates the class, runs a few functions then prints to <code>stdout</code>. The main method does this by first taking in one parameter (file name) and then reading the file and doing some operations on it. So I want to test that it outputs the correct data for each file - should each of these test cases reside in the same test method, or separate ones? I opted for separate ones, but only down to the sole reason that I feel as though these are more prone to failure than the previous method. So if this is the correct way to test it, what should the test method be called? <code>testMain1</code>, <code>testMain2</code> etc? What is the 'accepted' nomenclature?</p>
  
  <p>My <code>main</code> test case:</p>
  
  <pre><code>private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
  private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
  
  @Before
  public void setUpStreams() {
      System.setOut(new PrintStream(outContent));
      System.setErr(new PrintStream(errContent));
  }
  
  @After
  public void cleanUpStreams() {
      System.setOut(null);
      System.setErr(null);
  }
  
  @Test
  public void testMain4in() {
      SimmonsPS5.main(new String[]{"4.in"});
      assertEquals(47485, Integer.parseInt(outContent.toString().trim()));
  }
  
  @Test
  public void testMain5in() {
      SimmonsPS5.main(new String[]{"5.in"});
      assertEquals(1667, Integer.parseInt(outContent.toString().trim()));
  }
  
  @Test
  public void testMain6in() {
      SimmonsPS5.main(new String[]{"6.in"});
      assertEquals(45871, Integer.parseInt(outContent.toString().trim()));
  }
  
  @Test
  public void testMain7in() {
      SimmonsPS5.main(new String[]{"7.in"});
      assertEquals(324, Integer.parseInt(outContent.toString().trim()));
  }
  
  @Test
  public void testMain8in() {
      SimmonsPS5.main(new String[]{"8.in"});
      assertEquals(1057527, Integer.parseInt(outContent.toString().trim()));
  }
  </code></pre>
  
  <p>Note that the output to <code>stdout</code> is a single integral number.</p>