Testing JUnit4@Test注释实际上做什么

Testing JUnit4@Test注释实际上做什么,testing,junit,junit4,Testing,Junit,Junit4,@Test实际上做什么?我有一些没有它的测试,它们运行良好 我的课从 public class TransactionTest extends InstrumentationTestCase { 该测试通过以下任一方式运行: public void testGetDate() throws Exception { 或 编辑:有人指出我可能正在使用JUnit3测试,但我认为我正在使用JUnit4: 它将方法标识为测试方法。JUnit调用该类,然后调用带注释的方法 如果发生异常,则测试失败;但是

@Test实际上做什么?我有一些没有它的测试,它们运行良好

我的课从

public class TransactionTest extends InstrumentationTestCase {
该测试通过以下任一方式运行:

public void testGetDate() throws Exception {

编辑:有人指出我可能正在使用JUnit3测试,但我认为我正在使用JUnit4:


它将方法标识为测试方法。JUnit调用该类,然后调用带注释的方法

如果发生异常,则测试失败;但是,您可以指定应发生异常。否则,测试将失败(异常测试-逆向测试):

@测试(预期=Exception.class)-如果方法未引发命名异常,则测试失败

您还可以设置时间限制,如果函数未在分配的时间内完成,则函数将失败:


@Test(timeout=500)-如果该方法花费的时间超过500毫秒,则失败。

在JUnit4中,
@Test
注释用于告诉JUnit特定的方法是测试。相反,在JUnit3中,如果每个方法的名称以
test
开头,并且它的类扩展了
TestCase
,那么每个方法都是一个测试

@Test 
public void method()

@Test => annotation identifies a method as a test method.
@Test(expected = Exception.class) => Fails if the method does not throw the named exception.
@Test(timeout=100)  =>  Fails if the method takes longer than 100 milliseconds.

@Before 
public void method() =>This method is executed before each test. It is used to prepare the test environment (e.g., read input data, initialize the class).

@After 
public void method() => This method is executed after each test. It is used to cleanup the test environment (e.g., delete temporary data, restore defaults). It can also save memory by cleaning up expensive memory structures.

@BeforeClass 
public static void method() => This method is executed once, before the start of all tests. It is used to perform time intensive activities, for example, to connect to a database. Methods marked with this annotation need to be defined as static to work with JUnit.

@AfterClass 
public static void method() =>  This method is executed once, after all tests have been finished. It is used to perform clean-up activities, for example, to disconnect from a database. Methods annotated with this annotation need to be defined as static to work with JUnit.

@Ignore => Ignores the test method. This is useful when the underlying code has been changed and the test case has not yet been adapted. Or if the execution time of this test is too long to be included.
我假设InstrumentationTestCase扩展了junit.framework.TestCase。这意味着您正在JUnit3测试中使用JUnit4注释。在这种情况下,运行测试的工具(您的IDE或构建工具,如Ant或Maven)决定is是否识别
@Test
注释。您可以通过将
testGetDate()
重命名为不以
test
开头的内容来验证这一点,例如
shouldReturnDate()
。如果您的工具仍然运行17个测试,那么您知道它在JUnit3测试中支持JUnit4注释。如果它运行了16个测试,那么您就知道
@Test
注释只是一个不起任何作用的flashbang


JUnit4仍然提供JUnit3的类(JUnit.framework包)。这意味着您可以将JUnit 3样式的测试与JUnit 4一起使用。

在JUnit中,注释用于根据测试执行的角度赋予方法或类意义。一旦将@Test annotation与方法一起使用,那么该方法就不再是一个普通的方法,而是一个测试用例,它将由IDE作为测试用例执行,JUnit将根据您的断言根据传递的测试用例或邮件显示其执行结果


如果您作为初学者开始使用JUnit,请在此处查看简单的JUnit教程-

Hmm。无论是否使用\@Test,它都会显示17个测试中的17个运行。我在类中有17个函数,因此它们似乎在没有\@Test的情况下运行@Test注释表明测试方法所附加的public void方法可以作为测试用例运行,而无需扩展测试用例。我假设(因为没有显示您的测试类),您的类正在扩展TestCase,即JUnit3。在JUnit4中,您不必这样做。试着脱掉它,看看是否所有的测试都通过了。它还允许添加其他注释,如@Before/@BeforeClass和@After/@AfterClass。JUnit3没有这个选项。它更灵活。不仅您不必在JUnit4样式的测试中扩展TestCase,而且如果您这样做,许多功能(如规则、超时和预期的异常)将无法工作。不要扩展junit.framework.TestCase或使用junit.framework包中的任何其他类。junit 4仍然提供junit 3的类。
@Test 
public void method()

@Test => annotation identifies a method as a test method.
@Test(expected = Exception.class) => Fails if the method does not throw the named exception.
@Test(timeout=100)  =>  Fails if the method takes longer than 100 milliseconds.

@Before 
public void method() =>This method is executed before each test. It is used to prepare the test environment (e.g., read input data, initialize the class).

@After 
public void method() => This method is executed after each test. It is used to cleanup the test environment (e.g., delete temporary data, restore defaults). It can also save memory by cleaning up expensive memory structures.

@BeforeClass 
public static void method() => This method is executed once, before the start of all tests. It is used to perform time intensive activities, for example, to connect to a database. Methods marked with this annotation need to be defined as static to work with JUnit.

@AfterClass 
public static void method() =>  This method is executed once, after all tests have been finished. It is used to perform clean-up activities, for example, to disconnect from a database. Methods annotated with this annotation need to be defined as static to work with JUnit.

@Ignore => Ignores the test method. This is useful when the underlying code has been changed and the test case has not yet been adapted. Or if the execution time of this test is too long to be included.