简体   繁体   中英

Naming conventions for test cases in Eclipse

I encountered the weirdest issue with Eclipse Kepler, Service Release 1. I set up a class to implement some test cases by extending junit.framework.TestCase , like so:

import junit.framework.TestCase;

public class PublicTests extends TestCase {

    @Override
    public void setUp(){
        ...
    }

Then I added some public void test case methods, all of the names of which started with the prefix test . For example, testBasicAdd . Then, I added another public void test case method, named stressTestForAdd() , and when I ran all the test cases, that particular one was not even recognized as a test case and therefore not run. I made sure that it was actually not recognized as a test case by deleting all other methods, thus creating an instance of TestCase with stressTestForAdd as its only test case, and when I re-ran the suite I received a neat AssertionError stating that I hadn't authored any test cases in my file. When I added the prefix test to my method, creating the unwieldy name testStressTestForAdd , the test case was recognized.

I haven't encountered the need to have the test prefix in a jUnit test case anywhere in the relevant literature, not even as a convention. Is this a bug of Eclipse, perhaps? Can anybody shed some light?

In early versions of JUnit eg JUnit 3, methods are executed as tests as long as the method name begins with test

eg

 public void testGetName()

In JUnit 4, support for annotations was introduced, thus in JUnit 4, you may name your test method anything you desire as long as you annotate it with @Test

eg

@Test
public void getName()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM