Believe in yourself.

Pytest学习笔记(三) 在代码中运行pytest

前面介绍的是在cmd中执行pytest,平常我们一般都是通过编译器(如pycharm)来编写用例的,写完用例后,需要调试看看是否能运行,如果每次都切换到cmd中执行,太麻烦。

因此,这一节来说下怎么在代码中执行pytest。

需要先导入pytest,并通过pytest.main()来执行。

import pytest
class TestClass(object):
    def test_one(self):
        x = "this"
        assert 'h' in x

    def test_two(self):
        x = "hello"
        assert hasattr(x, 'check')

if __name__ == '__main__':
    pytest.main()

默认是执行当前脚本所在目录下的所有用例。

当然,也可以加参数来指定运行规则,参数必须放在列表或元组中

import pytest
class TestClass(object):
    def test_one(self):
        x = "this"
        assert 'h' in x

    def test_two(self):
        x = "hello"
        assert hasattr(x, 'check')

    def test_three(self):
        assert 3 == 5

if __name__ == '__main__':
    pytest.main(['-q','--maxfail=1','test_class.py'])

 

posted @ 2019-04-18 19:23  eastonliu  阅读(1430)  评论(0编辑  收藏  举报