Pytest笔记_入门

1、查看版本号

pip show pytest

pytest --version

2、pytest运行规则:

查找当前目录及其子目录下以test_*.py或*_test.py文件,找到文件后,在文件中找到以test开头函数并执行

#单一的测试用例
def fun(x):

    return x+1



def test_answer():

    assert fun(3)==5
# 写个测试类,包含多个测试方法
class TestClass:
    def test_one(self):
        x="this"
        assert 'h' in x

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

pytest -q test_sample.py

显示简单结果,传递文件名来运行模块

cmd执行pytest用例的三种方法:

*pytest

*py.test

*python -m pytest

执行规则:

pytest 文件夹名/

pytest 脚本名称.py

pytest -k "关键字"

pytest -x 脚本名称.py   ——遇到错误时停止调试

pytest --maxfail=num   ——当用例错误个数达到指定数量时,停止测试

3、pytest用例设计原则

*文件名以test_*.py或*_test.py文件

*以test_开头的函数

u*以Test开头的类

*所有的包package必须要有__init__.py文件

4、pytest -h获取帮助文档

hasattr() 函数用于判断对象是否包含对应的属性

5、pytest在pycharm运行方式

(1)复习unittest运行方式

import unittest
class WEBCase(unittest.TestCase):
    def testloginSuccess(self):

              ......

if __name__=="__main__":
    unittest.main()

(2)以pytest方式运行,要改工程默认的运行器

# 写个测试类,包含多个测试方法
import pytest

class TestSample():
    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(['-q','test_sample.py'])

猜你喜欢

转载自blog.csdn.net/baidu_37837739/article/details/84201216