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'])

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM