webui-aototest:pytest

pytest是Python的一款单元测试框架:

1.使用规则:

(1)pytest文件的文件名形式固定为①test__.py,②__test.py;

(2)pytest文件中测试类命名时,必须用Test开头:Test__.py;

(3)pytest文件中方法与函数命名必须要用 test__ 开头,函数没有__test.py的形式。

2.运行:

(1)命令行:pytest [文件名]

(2)模块运行:file->settings->tools->testing,选择default test runner为pytest;

                           点击测试类或测试函数前的执行按钮。

(3)pytest.main():file->settings->tools->testing,选择default test runner为unittest;

if __name__=='__main__':
    pytest.main(["testname.py::testclass::testfunction","-vs"])

3.其他

(1)函数级别setup()/teardown():运行于测试方法的始末,即:运行一次测试函数会运行一次setup和teardown;

  1. def setup(self):

  2. print("------->setup_method")

(2)类级别setup()/teardown():运行于测试类的始末,即:在一个测试内只运行一次setup_class和teardown_class,不关心测试类内有多少个测试函数;

(3)pytest-html可以生成测试报告文件;

        pytest.main ( ‘–alluredir’, ‘report/result’, ‘TestDemo01.py’])会在当前文件夹创建一个report文件夹,在report文件夹下创建result,生成测试报告。

        Allure是一款轻量级并且非常灵活的开源测试报告框架。 它支持绝大多数测试框架, 例如TestNG、Pytest、JUint等。它简单易用,易于集成。

        【关于测试报告的生成后续再了解】

(4)@pytest.fixture(scope='',autouse='')

  1. scope:被标记方法的作用域

  2. function(default):作用于每个测试方法,每个test都运行一次

  3. "class":作用于整个类,每个class的所有test只运行一次

  4. "module":作用于整个模块,每个module的所有test只运行一次

  5. "session:作用于整个session(慎用),每个session只运行一次

  6. params:(list类型)提供参数数据,供调用标记方法的函数使用

  7. autouse:是否自动运行,默认为False不运行,设置为True自动运行

#conftest.py

import pytest


@pytest.fixture(scope='session', autouse=True)
def realize_session():
    print('session之前')
    yield
    print('session之后')


@pytest.fixture(scope='package', autouse=True)
def realize_package():
    print('package之前')
    yield
    print('package之后')


@pytest.fixture(scope='module', autouse=True)
def realize_module():
    print('module之前')
    yield
    print('module之后')


@pytest.fixture(scope='class', autouse=True)
def realize_class():
    print('class之前')
    yield
    print('class之后')


@pytest.fixture(scope='function', autouse=True)
def realize_function():
    print('function之前')
    yield
    print('function之后')


@pytest.fixture(scope='function', name='aa')
def realize_auto_flase():
    print('autouse=false-function之前')
    yield
    print('autouse=false-function之后')

(5)@pytest.mark.skipif(condition, reason="xxx") :跳过条件condition的测试方法

(6)@pytest.mark.parametrize(argnames,argvalues):方便测试函数对测试属于的获取。

        当参数为一个时格式:[value];当参数个数大于一个时,格式为:[(param_value1,param_value2.....),(param_value1,param_value2.....)]

        参数值为N个,测试方法就会运行N次。

import pytest

class Test_try():
    @pytest.mark.parametrize("num",[1,2])
    def test_input(self,num):
        print(num)
        assert num == 1 or 2

(7)pytest --junitxml=path:生成 JUnitXML 格式的结果文件,这种格式的结果文件可以被Jenkins或其他CI工具解析。

(8)conftest.py文件:它主要是实现数据(fixture)共享的文件,名字是固定的。

        1- 第一,conftest.py文件当中,它储存的都是fixture,就是给用例提供做前置准备工作和后置清理工作的一个东西;

        2- 第二,conftest.py文件可以将它的fixtures共享到它自己目录下的所有用例,用例当中如果使用fixture的话,是不需用导入conftest.py这个文件的,会直接自动去查找;

        3- 第三,conftest.py它是属于层级共享的,也就是说,一个自动化项目当中,可以在不同的包下面去创建conftest.py这个文件。

        运行原则:就近原则,如果当前包下没有conftest.py就会往上一层找

参考链接:

百度安全验证:python测试框架-pytest

pytest基础知识一_张三♞的博客-CSDN博客_pytest

Python测试框架之pytest详解_你若安好我便天晴的博客-CSDN博客_python测试框架之pytest详解

pytest.fixture详解_虚心+坚持+感恩的博客-CSDN博客_pytest.fixture

pytest.mark.parametrize()基本用法_你若安好我便天晴的博客-CSDN博客_pytest.mark.parametrize

allure简介与使用_偷懒的肥猫的博客-CSDN博客_allure

Pytest和Allure测试框架-超详细版+实战_测试之道.的博客-CSDN博客_allure pytest

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值