Python基础篇:单元测试unittest

一:简介

  • 测试类必须继承unittest.TestCase,测试方法名必须以test开头才算测试方法)

  • 测试方法如果是以test开头后面跟数字,测试执行顺序就按照数字顺序来执行,否则按照方法的前后声明来执行

  • 一个测试方法叫一个 TestCase

  • 一个测试类叫 TestSuite

  • 测试加载器 TestLoader 用于加载多个TestCase

  • 文本测试运行器 TextTestRunner 将测试结果以文本的形式输出到控制台

  • HTML测试运行器 HTMLTestRunner 将测试结果写到.html文件中

序号断言方法断言描述
1assertEqual(arg1, arg2, msg=None)验证arg1=arg2,不等则fail
2assertNotEqual(arg1, arg2, msg=None)验证arg1 != arg2, 相等则fail
3assertTrue(expr, msg=None)验证expr是true,如果为false,则fail
4assertFalse(expr,msg=None)验证expr是false,如果为true,则fail
5assertIs(arg1, arg2, msg=None)验证arg1、arg2是同一个对象,不是则fail
6assertIsNot(arg1, arg2, msg=None)验证arg1、arg2不是同一个对象,是则fail
7assertIsNone(expr, msg=None)验证expr是None,不是则fail
8assertIsNotNone(expr, msg=None)验证expr不是None,是则fail
9assertIn(arg1, arg2, msg=None)验证arg1是arg2的子串,不是则fail
10assertNotIn(arg1, arg2, msg=None)验证arg1不是arg2的子串,是则fail
11assertIsInstance(obj, cls, msg=None)验证obj是cls的实例,不是则fail
12assertNotIsInstance(obj, cls, msg=None)验证obj不是cls的实例,是则fail

二:案例

testcase1.py

from unittest import TestCase


class TestCase1(TestCase):
    def test01(self):
        print('TestCase2:test01')

testcase2.py

import os
import unittest
from unittest import TestCase
from testcase1 import TestCase1


class User:
    pass


# 必须继承TestCase
# 测试方法名可以'test'开头,以数字作为编号
class TestCase2(TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        print('1. setUpClass init 多个案例最先执行一次,优先级高于setUp')

    @classmethod
    def tearDownClass(cls) -> None:
        print('4. tearDownClass destroy 多个测试方法最后只会执行一次')

    def setUp(self) -> None:
        print('2. setUp init 每个测试方法都会执行一次')

    def tearDown(self) -> None:
        print('3. tearDown destroy 每个测试方法都会执行销毁')

    def test00(self):
        print('test00')

    def test01(self):
        print('test01')
        first = 'a'
        second = 'a'
        msg = 'assert error'

        self.assertEqual(first, second, msg)
        self.assertNotEqual(first, second, msg)
        self.assertTrue(first, msg)
        self.assertFalse(first, msg)
        self.assertIsNone(first, msg)
        self.assertIsNotNone(first, msg)

        user1 = User()
        user2 = user1
        # user1 is user2
        self.assertIs(user1, user2)
        self.assertIsNot(user1, user2)
        self.assertIn(1, [1, 2, 3])
        self.assertNotIn(1, [2, 3, 4])

        self.assertIsInstance(1, int)
        self.assertNotIsInstance([1, 2, 3], dict)

        self.assertGreater(2, 1, '2 > 1')
        self.assertGreaterEqual(1, 1, '1 >= 1')

        self.assertLess(1, 2, '1 < 2')
        self.assertLessEqual(1, 2, '1 <= 2')

    @unittest.skipIf(2 > 1, 'skipIf:condition为true跳过')
    def test03(self):
        print('test03')

    @unittest.skip('跳过该测试案例skip')
    def test02(self):
        print('test02')

    @unittest.skipUnless(2 > 1, 'skipUnless: condition为true执行,为false不执行')
    def test04(self):
        print('test04')


if __name__ == '__main__':
    # 运行方式一:运行当前文件中以test开头的函数
    unittest.main()

    # 运行方式二:运行多个测试类下的测试方法
    loader = unittest.TestLoader()
    testsuite1 = loader.loadTestsFromTestCase(TestCase1)
    testsuite2 = loader.loadTestsFromTestCase(TestCase2)

    suite = unittest.TestSuite([testsuite1, testsuite2])
    runner = unittest.TextTestRunner()
    runner.run(suite)

    # 方式三:运行指定目录下的所有以test开头的py文件
    tests = unittest.defaultTestLoader.discover(os.path.dirname(__file__), pattern='test*.py')
    suite = unittest.TestSuite()
    suite.addTest(tests)
    runner = unittest.TextTestRunner()
    runner.run(suite)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风流 少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值