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

版权声明:本文为redrose2100原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。