5

I'm having difficulties finding a hard definition of the word "test case". Some sources claim that a test case is a class that extends TestCase. Other sources claim that a test case is a single test method. The JUnit documentation is unclear, it appears to me the words "test case" and "test" mean the same:

The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case. To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. Any exceptions thrown by the test will be reported by JUnit as a failure. If no exceptions are thrown, the test is assumed to have succeeded.

So what exactly is a "test case", and what is its relationship to a "test"?

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
  • 3
    I think you're unable to find a "hard definition" because there isn't one. Does it really matter? If you're having difficulties communicating what you mean with the "vague" terminology, just be clear about _your_ usage. – Matt Ball Mar 07 '11 at 14:36
  • 1
    I think (**warning**: speculation ahead) some of the confusion arrose because at some point someone realised they could refactor some of the JUnit code to introduce an abstract base class (or interface) and so use the Composite design pattern to simplify some code, resulting in `Test` and `TestCase` became less distinct in meaning. – Raedwald Mar 07 '11 at 15:21

2 Answers2

1

I'd point you over to the JUnit documentation on TestCase. In it, it describes three conditions for what it denotes a TestCase:

  1. implement a subclass of TestCase
  2. define instance variables that store the state of the fixture
  3. initialize the fixture state by overriding setUp()
  4. clean-up after a test by overriding tearDown().

The setUp and tearDown portion I think are critical to understanding here. It's not just simply that a test case is but one annotated method. The test case is that method plus the initialization and destruction of the frame on which a test will be run.

So to answer your question, a test is one annotated method which attempts to verify the workings of a single method while a test case is that test plus the frame in which it will be run.

wheaties
  • 35,646
  • 15
  • 94
  • 131
  • It also says "A test case defines the fixture to run multiple tests. [...] Each test runs in its own fixture [...] For each test implement a method which interacts with the fixture." That sounds to me like "method = test" and "class = test case"... – fredoverflow Mar 07 '11 at 14:42
  • @FredOverflow I think the fixture in this case is the setUp and tearDown. Those are supposed to be the invariants of all the labled tests. – wheaties Mar 07 '11 at 14:45
  • I just realized that `TestCase` implements `Test`. Does that imply that a test case is one possible kind of test, in the sense that every test case is also a test? More confusion :) – fredoverflow Mar 07 '11 at 15:07
  • @FredOverflow I think you're confusing the semantics of "TestCase" with "test case." One refers to a class within the JUnit hierarchy, the other refers to the conceptual formulations of unit testing in general. – wheaties Mar 07 '11 at 15:12
  • Maybe I have been "spoiled" by good naming conventions? I would not name a class `TestCase` unless its objects (or objects from classes derived from it) represented test cases. – fredoverflow Mar 07 '11 at 15:24
  • @FredOverflow instances of classes that extend `junit.framework.TestCase` do represent test cases! Each instance runs one method with a name starting with "test" (running `setUp()` before the test and `tearDown()` after the test). When you tell your IDE to run a class that extends `TestCase` it creates a suite to run the tests defined by your class (specifically, it creates a `TestSuite` that contains one `TestCase` instance per test method). – NamshubWriter Mar 21 '11 at 02:09
  • As for why `TestCase` implements `Test`, see http://junit.sourceforge.net/doc/cookstour/cookstour.htm – NamshubWriter Mar 21 '11 at 02:18
1

In the JUnit context, "test case" can mean a class which contains related tests (in JUnit 4, you don't have to extend TestCase anymore) or a single test method. There really isn't a hard definition of the term.

In my book, a test case is the answer to this question: Does the piece of code under test behave as expected?

Test cases can be tiny:

Foo foo = new Foo();
assertEquals( "...", foo.bar(5) );

or they can setup a database, will it with test data, create an object model and then invoke a couple of methods to verify that modifications of the object model work as expected.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820