单元测试基础知识

测试方法命名

  1. 简单测试:以test开头,以业务方法结尾。如业务方法名:isToday,测试方法可命名为:testIsToday;
  2. 复杂测试:期望结果_测试场景。

测试方法声明

        必须是public修饰的,必须没有返回值,必须没有参数,必须使用@Test注解修饰。

运行测试方法

  1. 选中测试方法名 -->右键--> Run '测试方法名' 运行选中测试方法;

  2. 选中类名 -->右键 -->Run '测试类名' 运行选中类所有的测试方法;

  3. 选中模块名 -->右键 -->Run 'All Tests' 运行选中模块中所有测试类中的所有测试方法。

测试结果

  1. 绿色:代表测试成功,没有问题
  2. 红色:代表测试出现问题

注解

  1. @Before:在每一个测试方法执行之前执行一次
  2. @After:会在每一个测试方法执行之后执行一次
  3. @BeforeClass:在所有测试方法执行之前执行一次,全局只会执行一次,且是第一个运行
  4. @AfterClass: 在所有测试方法执行之后执行一次,全局只会执行一次,且是最后一个运行
  5. @Test:测试方法

  6. @Ignore 忽略此方法

断言方法        

class AssertionsTest {

    @Test
    fun testAssertNull() {
        val str: String? = null
        assertNull(str)
    }

    @Test
    fun testAssertNotNull() {
        val str = "hello Java!!"
        assertNotNull(str)
    }

    @Test
    fun testAssertEqualsLong() {
        val long1: Long = 2
        val long2: Long = 2
        assertEquals(long1, long2)
    }

    @Test
    fun testAssertEqualsDouble() {
        // test case is successful as double1 and double 2
        // differ by 0.001 which is less than our specified delta
        val double1 = 1.236
        val double2 = 1.237
        val delta = 0.002
        assertEquals(double1, double2, delta)
    }

    @Test
    fun testAssertTrue() {
        val list: List<String> = ArrayList()
        assertTrue(list.isEmpty())
    }

    @Test
    fun testAssertFalse() {
        val list: MutableList<String> = ArrayList()
        list.add("hello")
        assertFalse(list.isEmpty())
    }

    @Test
    fun testAssertSame() {
        val str1 = "hello world!!"
        val str2 = "hello world!!"
        assertSame(str2, str1)
    }

    @Test
    fun testAssertNotSame() {
        val str1 = "hello world!!"
        val str3 = "hello Java!!"
        assertNotSame(str1, str3)
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值