`

JUnit4入门学习

    博客分类:
  • j2se
阅读更多

JUnit4入门学习

 

1 继承TestCase类的简单测试

1.1 简单测试

1.1.1 继承TestCase

public class Test1 extends TestCase

A test case defines the fixture to run multiple tests. To define a test case

1.1.2 编写测试方法

public void test**() {

}

测试的方法的格式是固定的

1.1.3 如果需要为每个测试方法前执行什么操作,则需要重写setup方法

protected void setUp() {

}

每个测试方法前都会执行这个方法

setUp

testEquals

setUp

testAdd

1.1.4 如果需要为每个测试方法后执行什么操作,则需要重写

protected void tearDown() throws Exception {

}

setUp

testEquals

tearDown

setUp

testAdd

teardown

1.1.5 使用断言来判断测试的结果

assertEquals(12, 12);

assertTrue(result == 5);

1.1.6 使用命令行语句执行各个Test

public static void main (String[] args) {

junit.textui.TestRunner.run(suite());

}

public static Test suite() {

return new TestSuite(SimpleTest.class);

}

TestRunner : A command line based tool to run tests

Test: A Test can be run and collect its results.

TestSuite: A TestSuite is a Composite of Tests. It runs a collection of test cases.

1.2 补充

Fail(string msg)方法:利用JUnit Test跑测试程序的时候,显示的失败提示

 

2 使用annotations的简单测试

2.1 简单测试

2.1.1 新建一个普通的java类

2.1.2 编写测试方法,保证测试方法的根式是

@Test

public void ***() {

}

测试方法的名称可以不以test开头了

The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case. To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. Any exceptions thrown by the test will be reported by JUnit as a failure. If no exceptions are thrown, the test is assumed to have succeeded

The Test annotation supports two optional parameters.The first, expected, declares that a test method should throw an exception. If it doesn't throw an exception or if it throws a different exception than the one declared, the test fails. 

The second optional parameter, timeout, causes a test to fail if it takes longer than a specified amount of clock time (measured in milliseconds).

2.1.3 测试方法先添加方法

@Before

public void *() {

}

方法名称可以不叫setup

2.1.4 测试方法后添加方法

@After

public void after()

{ }

2.1.5 使用命令行语句执行各个Test

public static void main (String[] args) {

junit.textui.TestRunner.run(suite());

}

public static junit.framework.Test suite() {

return new JUnit4TestAdapter(ListTest.class);

}

2.2 补充

2.2.1 @BeforeClass

Sometimes several tests need to share computationally expensive setup(like logging into a database). While this can compromise the independence of  tests, sometimes it is a necessary optimization. Annotating a public static void no-arg method with @BeforeClass causes it to be run once before any of the test methods in the class. The @BeforeClass methods of superclasses will be run before those the current class.

2.2.2 @Ignore

Sometimes you want to temporarily disable a test or a group of tests. Methods annotated with @Test that are also annotated with @Ignore will not be executed as tests. Also, you can annotate a class containing test methods with @Ignore and none of the containing  tests will be executed. Native JUnit 4 test runners should report the number of ignored tests along with the number of tests that ran and the number of tests that failed.

  For example:

@Ignore 

@Test

public void something() { ...}

@Ignore takes an optional default parameter if you want to record why a test is being ignored:@ Ignore("not ready yet") Test public void something() { ...}@Ignore can also be applied to the test class:

@Ignore 

public class IgnoreMe {

@Test 

public void test1() { ... }

@Test 

public void test2() { ... }

}


 

分享到:
评论

相关推荐

    junit4 快速入门

    junit4 快速入门 比较简单易学,欢迎前来学习

    junit5学习入门

    • JUnit 是一个开放的资源框架,用于编写和运行测试。 • 提供注释来识别测试方法。 • 提供断言来测试预期结果。 • 提供测试运行来运行测试。 • JUnit 测试允许你编写代码更快,并能提高质量。 • JUnit 优雅...

    junit 入门培训ppt

    我以前写的一个junit入门的培训ppt,传了大家参考一下

    Junit4学习笔记—入门篇.pdf

    以一个简单的DEMO介绍如何在Eclipse下使用JUnit4。供初次接触JUnit的同学学习

    Junit基础教程与学习

    此文档非常有助于刚刚接触junit的人来学习,图文并茂,很容易上手!

    junit4基础教程

    junit4基础教程合集,适合初学者学习,简单明了,易于学习,希望大家能认真阅读

    junit 4 资料

    它大大简化了开发人员执行单元测试的难度,特别是 JUnit 4 使用 Java 5 中的注解(annotation)使测试变得更加简单。 在单元测试前首先规划单元测试代码应放在什么地方。把它和被测试代码混在一起,这显然会照成混乱...

    junit4 单元测试源码

    此文件包含源代码(简单加减乘除),测试源码(junit4框架下的单元测试源码),入门级学习材料。Eclipse开发环境下

    jUnit入门教程 jUnit教程

    学习时候从网络上整理的juint资料,非常基础.

    JUnit入门,非常实用!

    JUnit入门,Junit入门学习,Junit初学者!

    单元测试JUnit4和DbUnit

    单元测试入门学习,和dbunit结合开发

    JUnit入门及简单使用

    很简单的说明,这是好的一个开始,希望大家能够看到,学习测试是很重要的。

    JUnit学习笔记之NetBeans入门篇

    JUnit学习笔记之NetBeans入门篇,一句话好东西

    Junit入门及应用

    Junit入门及应用,入门级别的学习资料。

    Junit实战第二版

    《JUnit实战(第2版)》从认识JUnit、不同的测试策略、JUnit与构建过程、JUnit扩展4个方面,由浅入深、由易到难地对JUnit展开了系统的讲解,包括探索JUnit的核心、软件测试原则、测试覆盖率与开发、使用stub进行粗...

    JUnit 5.pdf

    JUnit 5中文学习文档。JUnit 5是JUnit的下一代。目标是为JVM上的开发人员端测试创建一个最新的基础。这包括专注于Java 8及更高版 本,以及启用许多不同风格的测试。

    Junit实战第二版 中文完整版

    JUnit实战(第2版)本书从认识JUnit、不同的测试策略、JUnit与构建过程、JUnit扩展4个方面,由浅入深、由易到难地对JUnit展开了系统的讲解,包括探索JUnit的核心、软件测试原则、测试覆盖率与开发、使用stub进行粗粒度...

    Junit实战(第2版)

    本书从认识JUnit、不同的测试策略、JUnit与构建过程、JUnit扩展4个方面,由浅入深、由易到难地对JUnit展开了系统的讲解,包括探索JUnit的核心、软件测试原则、测试覆盖率与开发、使用stub进行粗粒度测试、使用mock...

    JUnit入门

    最基础的JUnit学习文档, 初学者可以下拿去看看

    junit基本教程

    junit基本教程 初级入门学习junit

Global site tag (gtag.js) - Google Analytics