Most visited

Recently visited

Added in API level 1

TestCase

public abstract class TestCase
extends Assert implements Test

java.lang.Object
   ↳ junit.framework.Assert
     ↳ junit.framework.TestCase
Known Direct Subclasses
Known Indirect Subclasses


测试用例定义了灯具运行多个测试。 定义一个测试用例

  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().
Each test runs in its own fixture so there can be no side effects among test runs. Here is an example:
 public class MathTest extends TestCase {
    protected double fValue1;
    protected double fValue2;

    protected void setUp() {
       fValue1= 2.0;
       fValue2= 3.0;
    }
 }
 
For each test implement a method which interacts with the fixture. Verify the expected results with assertions specified by calling assertTrue(String, boolean) with a boolean.
    public void testAdd() {
       double result= fValue1 + fValue2;
       assertTrue(result == 5.0);
    }
 
Once the methods are defined you can run them. The framework supports both a static type safe and more dynamic way to run a test. In the static way you override the runTest method and define the method to be invoked. A convenient way to do so is with an anonymous inner class.
 TestCase test= new MathTest("add") {
    public void runTest() {
       testAdd();
    }
 };
 test.run();
 
The dynamic way uses reflection to implement runTest(). It dynamically finds and invokes a method. In this case the name of the test case has to correspond to the test method to be run.
 TestCase test= new MathTest("testAdd");
 test.run();
 
The tests to be run can be collected into a TestSuite. JUnit provides different test runners which can run a test suite and collect the results. A test runner either expects a static method suite as the entry point to get a test to run or it will extract the suite automatically.
 public static Test suite() {
    suite.addTest(new MathTest("testAdd"));
    suite.addTest(new MathTest("testDivideByZero"));
    return suite;
 }
 

也可以看看:

Summary

Public constructors

TestCase()

无参数构造函数来启用序列化。

TestCase(String name)

用给定的名字构造一个测试用例。

Public methods

int countTestCases()

统计run运行的测试用例的数量(TestResult结果)。

String getName()

获取TestCase的名称

TestResult run()

运行此测试的便捷方法,使用默认的TestResult对象收集结果。

void run(TestResult result)

运行测试用例并在TestResult中收集结果。

void runBare()

运行裸露的测试序列。

void setName(String name)

设置TestCase的名称

String toString()

返回测试用例的字符串表示形式

Protected methods

TestResult createResult()

创建一个默认的TestResult对象

void runTest()

重写以运行测试并声明其状态。

void setUp()

设置夹具,例如打开网络连接。

void tearDown()

例如,关闭网络连接就可以拆卸灯具。

Inherited methods

From class junit.framework.Assert
From class java.lang.Object
From interface junit.framework.Test

Public constructors

TestCase

Added in API level 1
TestCase ()

无参数构造函数来启用序列化。 这个方法不打算在不调用setName()的情况下被凡人使用。

TestCase

Added in API level 1
TestCase (String name)

用给定的名字构造一个测试用例。

Parameters
name String

Public methods

countTestCases

Added in API level 1
int countTestCases ()

统计run运行的测试用例的数量(TestResult结果)。

Returns
int

getName

Added in API level 1
String getName ()

获取TestCase的名称

Returns
String the name of the TestCase

run

Added in API level 1
TestResult run ()

运行此测试的便捷方法,使用默认的TestResult对象收集结果。

Returns
TestResult

也可以看看:

run

Added in API level 1
void run (TestResult result)

运行测试用例并在TestResult中收集结果。

Parameters
result TestResult

runBare

Added in API level 1
void runBare ()

运行裸露的测试序列。

Throws
Throwable if any exception is thrown

setName

Added in API level 1
void setName (String name)

设置TestCase的名称

Parameters
name String: the name to set

toString

Added in API level 1
String toString ()

返回测试用例的字符串表示形式

Returns
String a string representation of the object.

Protected methods

createResult

Added in API level 1
TestResult createResult ()

创建一个默认的TestResult对象

Returns
TestResult

也可以看看:

runTest

Added in API level 1
void runTest ()

重写以运行测试并声明其状态。

Throws
Throwable if any exception is thrown

setUp

Added in API level 1
void setUp ()

设置夹具,例如打开网络连接。 在执行测试之前调用此方法。

Throws
异常

tearDown

Added in API level 1
void tearDown ()

例如,关闭网络连接就可以拆卸灯具。 该方法在执行测试后调用。

Throws
异常

Hooray!