UnitTest自动化框架中的测试套及runner的应用

【原文链接】UnitTest自动化框架中的测试套及runner的应用

1、默认的测试类中测试用例执行顺序

默认的测试类中的测试用例是按照测试用例名称的ASCII码值从小到大去执行的,不是按照用例的先后顺序执行的,如下,虽然顺序是乱的,但是执行仍然按照1,2,3的顺序执行

import unittest


class TestDemo01(unittest.TestCase):

    def test_03(self):
        print("in test_03...")

    def test_01(self):
        print("in test_01...")

    def test_02(self):
        print("in test_02...")


if __name__ == "__main__":
    unittest.main()

执行结果如下:

Ran 3 tests in 0.002s

OK
in test_01...
in test_02...
in test_03...

2、通过向测试套增加用例的方式组织用例

通过使用测试套和runner可以自定义用例的顺序,这个功能的作用不是很大,因为从自动化测试的角度来说用例与用例之间一般都是独立的,也就是后一个用例的执行不允许依赖前一个用例的执行,因此一般不会去关注用例的执行顺序,只要用例都执行完成了就OK了
如下,在一个目录下有两个文件,test01.py和test02.py,test01.py中存放着测试类及测试用例,代码如下:

import unittest


class TestDemo01(unittest.TestCase):

    def test_01(self):
        print("in test_01...")

    def test_02(self):
        print("in test_02...")
        
    def test_03(self):
        print("in test_03...")

test02.py中为组织测试用例的测试套文件,代码如下:

import unittest
from demo.test01 import TestDemo01

suite = unittest.TestSuite()
suite.addTest(TestDemo01("test_03"))
suite.addTest(TestDemo01("test_01"))
suite.addTest(TestDemo01("test_02"))
runner = unittest.TextTestRunner()
runner.run(suite)

执行结果如下:

in test_03...
in test_01...
in test_02...
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK

可以看出,使用测试套之后,用例的执行顺序按照往测试套中增加测试用例的顺序执行

3、通过向测试套中增加测试类的方式组织用例

可以通过向测试套中增加测试类的方式将一个测试类中的用例都加载进来,
比如 test01.py中有如下测试类

import unittest


class TestDemo01(unittest.TestCase):

    def test_01(self):
        print("in test_01...")

    def test_02(self):
        print("in test_02...")

    def test_03(self):
        print("in test_03...")

测试套中增加测试类的代码如下:

import unittest
from demo.test01 import TestDemo01

suite = unittest.TestSuite()
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestDemo01))
runner = unittest.TextTestRunner()
runner.run(suite)

执行结果如下

in test_01...
in test_02...
in test_03...
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK

4、通过discover执行执行目录下的所有测试用例

比如测试用例目录结构如下:

demo
  |----__init__.py
  |----suite.py
  |----test01.py
  |----test02.py

test01.py中代码如下:

import unittest


class TestDemo01(unittest.TestCase):

    def test_01(self):
        print("in test_01...")

    def test_02(self):
        print("in test_02...")

    def test_03(self):
        print("in test_03...")

test02.py中的代码如下:

import unittest


class TestDemo01(unittest.TestCase):

    def test_04(self):
        print("in test_04...")

    def test_05(self):
        print("in test_05...")

    def test_06(self):
        print("in test_06...")

suite.py中的代码如下:

import unittest

test_dir = "./"
discover = unittest.defaultTestLoader.discover(start_dir=test_dir, pattern="test*.py")

runner = unittest.TextTestRunner()
runner.run(discover)

执行结果如下

in test_01...
in test_02...
in test_03...
in test_04...
in test_05...
in test_06...
......
----------------------------------------------------------------------
Ran 6 tests in 0.000s

OK
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

redrose2100

您的鼓励是我最大的创作动力

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

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

打赏作者

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

抵扣说明:

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

余额充值