小门板儿

Menu

pytest–UNITTEST.TESTCASE 支持

pytest支持开箱即用的运行以 Python unittest 为基础的测试。这是为了在已有的在以unittest为基础的(unittest-based)测试上面进行扩充,使用pytest作为运行器

一个简单的unittest例子:

import unittest
def cacl(a,b):
    return a/b
class MyTest(unittest.TestCase):#类名需要以大写字母开头
    def setUp(self):
        print("每个用例执行之前都会执行")
    def tearDown(self):
        print("每个用例执行之后都会执行")
    @classmethod
    def setUpClass(cls):
        print("所有用例执行之前运行")
    @classmethod#类方法
    def tearDownClass(cls):
        print("所有用例执行之后运行")
    def test_a(self):
        res = cacl(1,2)
        self._testMethodDoc="正案例"#用于生成报告时用例描述的显示
        self.assertEqual(0.5,res,'success')#断言
    def test_b(self):
        '''反案例'''#用于生成报告是用例描述的第二种显示方式
        res = cacl(1,2)
        self.assertNotEqual(0.5, res, 'failed')
if __name__ == '__main__':
    unittest.main()#可以运行所有以test开头的用例,用例的执行顺序按照test后面的ascii码值的大小顺序,值小的先执行

运行用例:

​
D:\pytest\learn01>pytest --cache-clear -s -q g.py
所有用例执行之前运行
每个用例执行之前都会执行
每个用例执行之后都会执行
.每个用例执行之前都会执行
每个用例执行之后都会执行
F所有用例执行之后运行
​
===================== FAILURES =====================
_____________________ MyTest.test_b _____________________
​
self = <g.MyTest testMethod=test_b>
​
    def test_b(self):
        '''反案例'''#用于生成报告是用例描述的第二种显示方式
        res = cacl(1,2)
>       self.assertNotEqual(0.5, res, 'failed')
E       AssertionError: 0.5 == 0.5 : failed
​
g.py:22: AssertionError
=================== short test summary info ====================
FAILED g.py::MyTest::test_b - AssertionError: 0.5 == 0.5 : failed
1 failed, 1 passed in 0.24s
​

pytest会自动的收集 unittest.TestCase 的子类和在test.py 或是test.py文件中的测试方法。 几乎所有的unittest的功能都是支持的:

一、 开箱即用的优点

二、在unittest.TestCase 子类中使用pytest的功能

下面的pytest功能可以在unittest.TestCase的子类中使用:

下面这些pytest的功能不能使用,因为不同的设计观,这些功能可能永远不会支持:

三、使用标记在unittest.TestCase的子类中插入夹具

#content of conftest.py
#我们再下面定义了一个夹具,在测试中可以通过它的名字使用它
@pytest.fixture(scope="class")
def db_1():
    print("pytest fixture")
    return 1234

上面的代码定义了一个 db_1 函数,如果使用的话,每个测试类都会调用一次,。这种结构将夹具的编写和真实的代码分开,也使得只需要引用一下夹具的名字,就可以复用这个夹具。下面我们来写一个真实的unittest.TestCase来使用我们的夹具:

@pytest.mark.usefixtures("db_1")
class MyTest(unittest.TestCase):
    def test_method1(self):
        assert 1
 执行测试:
D:\pytest\learn01>pytest -s -q g/test_unittest.py
pytest fixture
.
1 passed in 0.11s

@pytest.mark.usefixtures(“db_1”)这个类修饰器确保了这个类只调用了一次 db_1,执行测试后发现,测试时运行了db_1(),输出了信息“pytest fixture”

四、 使用autouse的夹具和访问其他夹具

class MyTest(unittest.TestCase):
    @pytest.fixture(autouse=True)
    def initdir(self, tmpdir):
        tmpdir.chdir()  # change to pytest-provided temporary directory
        tmpdir.join("samplefile.ini").write("# testdata")
​
    def test_method(self):
        with open("samplefile.ini") as f:
            s = f.read()
            assert "testdata" in s

通过autouse标志,initdir夹具会被应用于它定义的类中的所有方法。这种方式是前面例子中使用 @pytest.mark.usefixtures(“initdir”) 的简便实现。

D:\pytest\learn01>pytest -s -q g/test_unittest1.py
.
1 passed in 1.14s

注意: unittest.TestCase不能直接接收夹具参数,在实现过程中这可能会影响pytest运行普通的 unittest 测试。 上面的usefixtures和autouse的例子是我们在unittest中使用夹具的正确方式。

参考文献:https://blog.csdn.net/nxy_wuhao/article/details/117221994?spm=1001.2014.3001.5501

— 于 共写了3088个字
— 标签:

评论已关闭。