Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test default value and setter in same test-case or separate test cases

Would you recommend doing any grouping of test cases within @Test methods, or have one @Test method per test scenario? For example, let's suppose that there are different ways to set the context in an application.

Is the following idea acceptable?

@Test public void testContextSetting() {     // Test default setting     assert(...)      // Test setting a context variable     assert(...)      ... } 

Or, would you rather suggest having it like this, having each method as atomic as possible:

@Test public void textDefaultSetting() {     // Test default setting     assert(...) }  @Test public void testSettingContextVar() {     // Test setting a context variable     assert(...)      ... } 

Any feedback would be appreciated.

like image 271
Ariod Avatar asked Jun 06 '11 12:06

Ariod


People also ask

What is the difference between a test case and a test suite?

A test case answers the question: What am I going to test? You develop test cases to define the things that you must validate to ensure that the system is working correctly and is built with a high level of quality. A test suite is a collection of test cases that are grouped for test execution purposes.

What is the purpose of a test suite?

In software development, a test suite, less commonly known as a validation suite, is a collection of test cases that are intended to be used to test a software program to show that it has some specified set of behaviors.

How to test methods in java?

write a main method on your class, and call your test method. To check by running just write a main method and call this method with arguments. If you want to have a test case, take a look at JUnit or Mokito to write a test. There should be a way to run parts or the code without writing a main method or a test-class.


1 Answers

I prefer having one test case per method.

First it is easier to see what cases are being tested if they are split into methods as opposed to looking for comments embedded in the code. Most IDEs will give you a summary of methods, so instead of saying "did I test edgecase XYZ?" and then hunting for a comment, or looking for the code that sets up that edgecase, you just look for the method named setupContextEdgeCaseXYZ().

A second reason is if you have multiple cases together one may fail and then the others never execute.

 testDefaultCase()  testInvalidInput()  testEdgeCase1()  testEdgeCase2() 

With this structure it would be easier to determine that the input checking is bad and edge case 2 is handled improperly, but the others are OK (and you may find out that two failing cases are related and the problem is diagnosed faster).

A third reason is you may accidentally leave values from a previous test set that invalidates a latter test in a inconspicuous way. A simple example:

@Test public void testMyMethod() {   //test default   String test = Foo.bar(null);   assertEquals("foo", test);    //test case 1   Foo.bar(aValue);   //Oops forgot to set value above, this passes regardless of    //what the above call does   assertEquals("foo", test); } 

By breaking cases apart you can avoid mistakes as above as that would turn into a compile error or warning.

like image 98
M. Jessup Avatar answered Sep 30 '22 06:09

M. Jessup