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

简介: UnitTest----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
目录
相关文章
|
29天前
|
前端开发 测试技术 API
测试金字塔:别再只盯着UI自动化了
测试金字塔:别再只盯着UI自动化了
278 116
|
29天前
|
敏捷开发 测试技术 API
测试金字塔:构建高效自动化测试策略的基石
测试金字塔:构建高效自动化测试策略的基石
225 116
|
29天前
|
设计模式 前端开发 测试技术
告别脆弱:构建稳定UI自动化测试的3个核心策略
告别脆弱:构建稳定UI自动化测试的3个核心策略
271 113
|
1月前
|
人工智能 自然语言处理 测试技术
从人工到AI驱动:天猫测试全流程自动化变革实践
天猫技术质量团队探索AI在测试全流程的落地应用,覆盖需求解析、用例生成、数据构造、执行验证等核心环节。通过AI+自然语言驱动,实现测试自动化、可溯化与可管理化,在用例生成、数据构造和执行校验中显著提效,推动测试体系从人工迈向AI全流程自动化,提升效率40%以上,用例覆盖超70%,并构建行业级知识资产沉淀平台。
从人工到AI驱动:天猫测试全流程自动化变革实践
|
1月前
|
人工智能 自然语言处理 JavaScript
利用MCP Server革新软件测试:更智能、更高效的自动化
MCP Server革新软件测试:通过标准化协议让AI实时感知页面结构,实现自然语言驱动、自适应维护的自动化测试,大幅提升效率,降低脚本开发与维护成本,推动测试左移与持续测试落地。
|
29天前
|
测试技术 API 数据库
测试金字塔:构建高效自动化测试策略的基石
测试金字塔:构建高效自动化测试策略的基石
245 114
|
1月前
|
SQL 安全 Linux
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
109 1
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
|
1月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
177 1
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
|
2月前
|
安全 Linux 网络安全
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
269 2
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架