public abstract class TestCase
extends Assert
implements Test
java.lang.Object | ||
↳ | junit.framework.Assert | |
↳ | junit.framework.TestCase |
Known Direct Subclasses |
Known Indirect Subclasses
ActivityInstrumentationTestCase<T extends
Activity>,
ActivityInstrumentationTestCase2<T extends
Activity>,
ActivityTestCase,
ActivityUnitTestCase<T extends
Activity>,
ApplicationTestCase<T extends
Application>,
LoaderTestCase,
ProviderTestCase<T extends
ContentProvider>,
ProviderTestCase2<T extends
ContentProvider>,
ServiceTestCase<T extends
Service>,
SingleLaunchActivityTestCase<T extends
Activity>,
SyncBaseInstrumentation
|
测试用例定义了灯具运行多个测试。 定义一个测试用例
TestCase
setUp()
tearDown()
.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; }
也可以看看:
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
|
int countTestCases ()
统计run运行的测试用例的数量(TestResult结果)。
Returns | |
---|---|
int |
String getName ()
获取TestCase的名称
Returns | |
---|---|
String |
the name of the TestCase |
TestResult run ()
运行此测试的便捷方法,使用默认的TestResult对象收集结果。
Returns | |
---|---|
TestResult |
也可以看看:
void run (TestResult result)
运行测试用例并在TestResult中收集结果。
Parameters | |
---|---|
result |
TestResult
|
void setName (String name)
设置TestCase的名称
Parameters | |
---|---|
name |
String : the name to set |
String toString ()
返回测试用例的字符串表示形式
Returns | |
---|---|
String |
a string representation of the object. |
TestResult createResult ()
创建一个默认的TestResult对象
Returns | |
---|---|
TestResult |
也可以看看:
void runTest ()
重写以运行测试并声明其状态。
Throws | |
---|---|
Throwable |
if any exception is thrown |