JUnit4
比之前的版本可爱多了。
编写Testcase
使用JUnit4
编写testcase
不再有继承Testcase
类的负担了。只要在测试方法加annotation
@org.junit.Test
就行了。
@org.junit.Test
public
void
test1(){
// ur test code here
}
一个类可以有多个这种加了@Test annotation
标注的测试方法。它们会在跑testcase
的时候无序无依赖的跑。
如果一个类中的Test
方法需要做一些初始化和清理工作,就需要用到@BeforeClass,
@AfterClass, @Before
和@After
这几个标注了。
@BeforeClass
public
void
overallInitial(){
// This method will run ONCE and ONLY ONCE before all
test methods in this class. Some initial works should be done here for all test
methods in this class.
}
@Before
public
void
initalTestCase(){
// This method run every time before for a test method.
Test method level initial work should be done here.
}
@After
public
void
clearupTestCase(){
// This method run every time after for a test method.
Test method level clearup work should be done here.
}
@AfterClass
public
void
overallClearup(){
// This method will run ONCE and ONLY ONCE after all test
methods in this class. Some clearup works should be done here for all test
methods in this class.
}
编写testcase
基本就这么简单。一切都用标注搞定就行了。
还有一些有用的标注:
忽略某个testcase
@Ignore("This case is not supported yet")
1s
内如果不能跑完就算失败
@Test(timeout=1000)
如果抛出IOException
则算测试成功
@Test(expected=IOException.class)
组织和运行Testcase
编写完Testcase
,一般需要将Testcase
组织成Testsuite
,这样可以一次跑多个Testcase
类。JUnit4
中组织Testcase
的方式有多种。
通过Annotation
最简单的还是通过annotation
。下面的类就是通过Annotation
来将多个Testcase
组织成一个Suite
package
junit.testsuite;
import
junit.testcase.JUnitTestCase;
import
junit.testcase.TestCase2;
import
org.junit.runner.RunWith;
import
org.junit.runners.Suite;
@RunWith(Suite.
class
)
@Suite.SuiteClasses({ JUnitTestCase.
class
, TestCase2.
class
})
public
class
AllTestsUsingAnnotation {
}
上面的类不需要代码,就俩标注就行了。一个@org.junit.runner.RunWith
,一个
@org.junit.runners.Suite
。@RunWith
表示这个类将以哪种形式来跑。后面的类型必须是Runner
接口的实现。在这里指定 为Suite
。@Suite.SuiteClasses
则可以包含多个test unit
类。
@Suite.SuiteClasses
中的类也可以指定另一个TestSuite
,这样就可以有多个包含层次了。不过其中的test unit
不能间接或者直接的包含当前类,否则就死循环了嘛。
这个类在Eclipse
里面是可以直接Run As JUnit Test
的。
通过手工创建TestSuite
如果不使用标注,可以手动创建TestSuite
。
package
junit.testsuite;
import
junit.framework.JUnit4TestAdapter;
import
junit.framework.Test;
import
junit.framework.TestSuite;
import
junit.testcase.TestCase3;
public
class
AllTestsUsingSuiteMethod {
public
static
Test suite() {
TestSuite suite =
new
TestSuite("Root Test");
// $JUnit-BEGIN$
suite.addTest(
new
JUnit4TestAdapter(TestCase3.
class
));
suite.addTest(
new
JUnit4TestAdapter(AllTestsUsingAnnotation.
class
));
// $JUnit-END$
return
suite;
}
}
上面的类创建了一个TestSuite
,同时向TestSuite
里增加了两个test unit
。其中第二个其实就是上面创建的一个TestSuite
。
如果在一个TestSuite
里面有重复的testcase
,那么将只有一个会被运行,重复的将被忽略。
这个类在Eclipse
里面是可以直接Run As JUnit Test
的。
运行Testcase
除了能够在Eclipse
里面运行之外,还可以在普通的程序甚至命令行中跑。
下面的code
就是通过应用程序跑TestCase
的。根据结果可以生成需要的表格等有组织的报表。
package
junit.testsuite;
import
junit.testcase.JUnitTestCase;
import
junit.testcase.TestCase2;
import
org.junit.runner.JUnitCore;
import
org.junit.runner.Result;
import
org.junit.runner.notification.Failure;
public
class
RunTestInMainApp {
public
static
void
main(String[] args) {
Result result =
JUnitCore.runClasses(JUnitTestCase.
class
,
TestCase2.
class
);
for
(Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
}
}
在命令行下也可以通过类似的方法跑。
java org.junit.runner.JUnitCore
junit.testcase.JUnitTestCase, junit.testcase.TestCase2
分享到:
相关推荐
junit tutorial e-book
JUnit is a framework for implementing testing in Java. It provides a simple way to explicitly test specific areas of a Java program, it is extensible and can be employed to test a hierarchy of program...
The tutorial gives a profound entry point in the essentials of unit testing with JUnit and prepares you for test-related daily work challenges. Style and approach This is an intelligible tutorial ...
本指南将引导您完成创建Spring应用程序,然后使用JUnit对其进行测试的过程。 你会建立什么 您将构建一个简单的Spring应用程序,并使用JUnit对其进行测试。 您可能已经知道如何编写和运行应用程序中各个类的单元测试...
tested as an example, and the resultant tutorial therefore took five times longer to digest than it should have. There is no need to learn to understand somebody else's data structures just to learn ...
Android单元测试(三):JUnit单元测试框架的使用 代码和测试代码在junit子package下面 Android单元测试在蘑菇街支付金融部门的实践 代码和测试代码在groupshare子package下面 Android单元测试(四):Mock以及Mockito...
1. JUnit 5开发人员指南 2. GitHub存储库 3.重要的Java开发人员指南 ...4. JUnit 4与JUnit 5的备忘单 5.下载PDF版的《 JUnit 5用户指南》。 https://github.com/RameshMF/ebooks/blob/master/junit5-user-guide.pdf
JUnit- https://courses.in28minutes.com/p/junit-tutorial-for-beginners Mockito- https: //courses.in28minutes.com/p/mockito-for-beginner-in-5-steps 安装工具 安装视频: https : //www.youtube.co
4 Eclipse Tutorial One-Page Startup Instruction VideoNotes 5 Teaching/Learning Java Effectively with Eclipse 6 JBuilder X Tutorial JBuilder 2005 Tutorial One Page Startup Instruction ...
Testing with JUnit4 Testing with Espresso Testing with UI Automator Measuring Test Coverage Unit Testing MonkeyRunner and the Test Monkey Java 8 Lambda Expressions Rx Basics Notifications Advanced ...
Testing with JUnit4 Testing with UI Automator Measuring Test Coverage Unit Testing MonkeyRunner and the Test Monkey Notifications Advanced Notifications Introducing GridLayout The Percent Support ...
Soapui 教程|Selenium|Maven|JUnit 概述 我已经有一段时间没有参与软件测试了。 该项目的目标是在这些教程中赶上各种最新技术。 也希望像我这样的人能从中受益,早点回去工作。 话题 本教程将涵盖以下主题。 JUnit ...
travis-ci-tutorial-java 只是为了学习如何在Java项目中使用travis-ci! 这是一个如何在GitHub上将Travis CI(和Codecov)与Java结合使用的最小工作示例。 它使用测试框架如何开始此存储库转到并启用存储库修复...
Integrate JUnit 4, the most widely used unit testing framework, into Eclipse Get to grips with how Eclipse can be used to develop web-based Java applications that employ Java Servlets and JavaServer ...
样本AEM项目模板 这是基于AEM的应用程序的项目模板。... ui.launcher:包含粘合代码,该粘合代码将ui.tests捆绑包(和相关捆绑包)部署到服务器并触发远程JUnit执行 ui.frontend:可选的专用前端构建机制(Angula
JUnit的 Log4J 引导程序 ReactJS 贡献者 Todos los教程从实践到实践,从完成到完成。 托尔多·洛斯·因特萨多斯和亲子游记 详细信息。 El material escrito empleando archivos md(MarkDown)。
本教程以使用junit测试用例针对不同功能的示例为例,说明了 Java api的用法。第1部分介绍ElasticSearch集群设置以及ES入门。 索引,文档类型,节点,分片/副本, 创建/检索/更新/删除文档模式映射,字段,过滤器,...
Cucumber教程先决条件: 在Eclipse中为功能文件安装Natural 依存关系< dependencies><...-- https://mvnrepository.com/artifact/info.cukes/cucumber-junit -->< dependency>< groupId>info.cukes</ group
面向对象的教程 这个存储库包含我在面向对象课程中经常使用的几个练习。... 每个练习的解决方案都是使用 TDD 技术在 Java 中实现的,Ant 作为构建工具,JUnit 用于实现自动化测试。 本材料根据许可进行许可。