美文网首页
python_ui自动化之公共数据

python_ui自动化之公共数据

作者: 雷阳洪 | 来源:发表于2020-06-19 14:01 被阅读0次

方案一

可以再模块上方创建一个公共字典,把需要共用的数据存入该字典中,在方法内部根据key从公共字典中取值

缺点:只能再模块内部传递公共数据,跨模块不生效

代码示例:


#!/usr/bin/env python
# -*- coding:utf-8 -*-

# 全字段正常流
import random

import requests
from config.config import API_URL
from tools.data.make_info import make_id
from tools.data.random_tool import random_tell, random_str_abc, random_number

# 创建一个公共字典,字典里边存放要存入的数据
d={"userName":random_str_abc(2) + str(random_number(min=0,max=9,step=1))}

def test_sign_up_1():
    # 方法内部根据关键字,例如:userName从公共字典中取值
    da = {"phone":random_tell(),"pwd": "shu123","rePwd": "shu123","userName": d["userName"]}
    r = requests.request("POST", API_URL + "/signup",json=da)
    print(r.text)
    print(r.request.body)
    assert "userName" in r.text


def test_login_1():
    # 方法内部根据关键字,例如:userName从公共字典中取值
    da = {
  "pwd": "shu123",
  "userName": d["userName"]
}
    r = requests.request("POST", API_URL + "/login", json=da)
    print(r.text)
    print(r.request.body)
    assert "token" in r.text

方案二

使用pytest框架提供的fixture函数来传递公共数据,集合conftest.py文件可以实现跨模块传递公共数据

代码示例:

conftest.py文件作用范围,当前包及其子包;
conftest.py解决fixture函数跨模块调用的问题
conftest.py:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pytest

from tools.data.random_tool import random_str_abc, random_number


# 只要加了@pytest.fixture()装饰器,该方法就是一个fixture函数。fixture执行完成之后,会把
# fixture方法的返回值存入方法名中,pytest用例中就可以根据fixture函数的方法名来使用该fixture的返回值
@pytest.fixture(scope="session")
def d():
    return {"userName":random_str_abc(2) + str(random_number(min=0,max=9,step=1))}

pytest用例:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

# 全字段正常流
import random

import pytest
import requests
from config.config import API_URL
from tools.data.make_info import make_id
from tools.data.random_tool import random_tell, random_str_abc, random_number

# pytest用例中就可以根据fixture函数的方法名来使用该fixture的返回值
def test_sign_up_1(d):
    # 方法内部根据关键字,例如:userName从公共字典中取值
    da = {"phone":random_tell(),"pwd": "shu123","rePwd": "shu123","userName": d["userName"]}
    r = requests.request("POST", API_URL + "/signup",json=da)
    print(r.text)
    print(r.request.body)
    assert "userName" in r.text

# pytest用例中就可以根据fixture函数的方法名来使用该fixture的返回值
def test_login_1(d):
    # 方法内部根据关键字,例如:userName从公共字典中取值
    da = {
  "pwd": "shu123",
  "userName": d["userName"]
}
    r = requests.request("POST", API_URL + "/login", json=da)
    print(r.text)
    print(r.request.body)
    assert "token" in r.text

fixture函数参数讲解

scope

scope 规定了fixture函数的执行时机,分别有以下四个值

  • session 项目启动
  • module 进入模块之前
  • class 进入类之前
  • function 执行一个方法之前

代码示例:
conftest.py:

import pytest
# session  项目启动
# module   进入模块之前
# class    进入类之前
# function 执行一个方法之前
@pytest.fixture(scope="session")
def scope_session():
    print("我是scope_session")

@pytest.fixture(scope="module")
def scope_module():
    print("我是scope_module")

@pytest.fixture(scope="class")
def scope_class():
    print("我是scope_class")

@pytest.fixture(scope="function")
def scope_function():
    print("我是scope_function")

test_demo_1.py:

#!/usr/bin/env python
# -*- coding:utf-8 -*-


class TestDemo1():


    def test_demo1(self,scope_session,scope_module,scope_class,scope_function):
        print("test_demo1")
    def test_demo2(self,scope_session,scope_module,scope_class,scope_function):
        print("test_demo2")


class TestDemo2():

    def test_demo3(self,scope_session,scope_module,scope_class,scope_function):
        print("test_demo3")

    def test_demo4(self,scope_session,scope_module,scope_class,scope_function):
        print("test_demo4")

test_demo_2.py:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#!/usr/bin/env python
# -*- coding:utf-8 -*-


class TestDemo3():


    def test_demo5(self,scope_session,scope_module,scope_class,scope_function):
        print("test_demo5")
    def test_demo6(self,scope_session,scope_module,scope_class,scope_function):
        print("test_demo6")


class TestDemo4():

    def test_demo7(self,scope_session,scope_module,scope_class,scope_function):
        print("test_demo7")

    def test_demo8(self,scope_session,scope_module,scope_class,scope_function):
        print("test_demo8")

执行测试用例:
pycharm terminal中执行下列命令
pytest -v -s -k "demo"

image.png

执行结果:

(venv) E:\softwaredata\python\gy-base-api-1911A>pytest -v -s -k "demo"
========================================================= test session starts ==========================================================
platform win32 -- Python 3.7.1, pytest-5.3.2, py-1.8.0, pluggy-0.13.1 -- e:\softwaredata\python\gy-base-api-1911a\venv\scripts\python.exe

cachedir: .pytest_cache
rootdir: E:\softwaredata\python\gy-base-api-1911A
collected 20 items / 12 deselected / 8 selected                                                                                         

test_case/test_demo_1.py::TestDemo1::test_demo1 我是scope_session
我是scope_module
我是scope_class
我是scope_function
test_demo1
PASSED
test_case/test_demo_1.py::TestDemo1::test_demo2 我是scope_function
test_demo2
PASSED
test_case/test_demo_1.py::TestDemo2::test_demo1 我是scope_class
我是scope_function
test_demo3
PASSED
test_case/test_demo_1.py::TestDemo2::test_demo2 我是scope_function
test_demo4
PASSED
test_case/test_demo_2.py::TestDemo3::test_demo5 我是scope_module
我是scope_class
我是scope_function
test_demo5
PASSED
test_case/test_demo_2.py::TestDemo3::test_demo6 我是scope_function
test_demo6
PASSED
test_case/test_demo_2.py::TestDemo4::test_demo7 我是scope_class
我是scope_function
test_demo7
PASSED
test_case/test_demo_2.py::TestDemo4::test_demo8 我是scope_function
test_demo8
PASSED

=================================================== 8 passed, 12 deselected in 0.37s ===================================================


总结

看上述执行结果,我们可以发现
scope="session"的fixture函数,只在项目启动的时候执行了一次;
scope="module"的fixture函数,只要执行到一个新的模块,都会执行该fixture函数;
scope="class"的fixture函数,只要执行到一个新的类,都会执行该fixture函数;;
scope="function"的fixture函数,只要执行到一个新的方法,都会执行该fixture函数;

autouse

自调用开关:

  • True 开启自调用
  • False 关闭自调用

代码示例:
conftest.py:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pytest



# session  项目启动
# module   进入模块之前
# class    进入类之前
# function 执行一个方法之前
@pytest.fixture(scope="session", autouse=True)
def scope_session():
    print("我是scope_session")

@pytest.fixture(scope="module", autouse=True)
def scope_module():
    print("我是scope_module")

@pytest.fixture(scope="class", autouse=True)
def scope_class():
    print("我是scope_class")

@pytest.fixture(scope="function", autouse=True)
def scope_function():
    print("我是scope_function")

test_demo_1.py:

#!/usr/bin/env python
# -*- coding:utf-8 -*-


class TestDemo1():


    def test_demo1(self):
        print("test_demo1")
    def test_demo2(self):
        print("test_demo2")


class TestDemo2():

    def test_demo3(self):
        print("test_demo3")

    def test_demo4(self):
        print("test_demo4")

test_demo_2.py:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#!/usr/bin/env python
# -*- coding:utf-8 -*-


class TestDemo3():


    def test_demo5(self):
        print("test_demo5")
    def test_demo6(self):
        print("test_demo6")


class TestDemo4():

    def test_demo7(self):
        print("test_demo7")

    def test_demo8(self):
        print("test_demo8")

执行结果

(venv) E:\softwaredata\python\gy-base-api-1911A>pytest -v -s -k "demo"
========================================================= test session starts ==========================================================
platform win32 -- Python 3.7.1, pytest-5.3.2, py-1.8.0, pluggy-0.13.1 -- e:\softwaredata\python\gy-base-api-1911a\venv\scripts\python.exe

cachedir: .pytest_cache
rootdir: E:\softwaredata\python\gy-base-api-1911A
collected 20 items / 12 deselected / 8 selected                                                                                         

test_case/test_demo_1.py::TestDemo1::test_demo1 我是scope_session
我是scope_module
我是scope_class
我是scope_function
test_demo1
PASSED
test_case/test_demo_1.py::TestDemo1::test_demo2 我是scope_function
test_demo2
PASSED
test_case/test_demo_1.py::TestDemo2::test_demo3 我是scope_class
我是scope_function
test_demo3
PASSED
test_case/test_demo_1.py::TestDemo2::test_demo4 我是scope_function
test_demo4
PASSED
test_case/test_demo_2.py::TestDemo3::test_demo5 我是scope_module
我是scope_class
我是scope_function
test_demo5
PASSED
test_case/test_demo_2.py::TestDemo3::test_demo6 我是scope_function
test_demo6
PASSED
test_case/test_demo_2.py::TestDemo4::test_demo7 我是scope_class
我是scope_function
test_demo7
PASSED
test_case/test_demo_2.py::TestDemo4::test_demo8 我是scope_function
test_demo8
PASSED

=================================================== 8 passed, 12 deselected in 0.25s ===================================================


总结:

我们把pytest用例的参数列表中,所有fixture函数的引用都删掉了,
但是所有的fixture函数还是被执行了,执行顺序完全遵循scope关键字的定义
如果我们关掉fixture自调用开关

conftest.py:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pytest

# session  项目启动
# module   进入模块之前
# class    进入类之前
# function 执行一个方法之前
@pytest.fixture(scope="session")
def scope_session():
    print("我是scope_session")

@pytest.fixture(scope="module")
def scope_module():
    print("我是scope_module")

@pytest.fixture(scope="class")
def scope_class():
    print("我是scope_class")

@pytest.fixture(scope="function")
def scope_function():
    print("我是scope_function")

执行结果:
(venv) E:\softwaredata\python\gy-base-api-1911A>pytest -v -s -k "demo"
========================================================= test session starts ==========================================================
platform win32 -- Python 3.7.1, pytest-5.3.2, py-1.8.0, pluggy-0.13.1 -- e:\softwaredata\python\gy-base-api-1911a\venv\scripts\python.exe

cachedir: .pytest_cache
rootdir: E:\softwaredata\python\gy-base-api-1911A
collected 20 items / 12 deselected / 8 selected                                                                                         

test_case/test_demo_1.py::TestDemo1::test_demo1 test_demo1
PASSED
test_case/test_demo_1.py::TestDemo1::test_demo2 test_demo2
PASSED
test_case/test_demo_1.py::TestDemo2::test_demo3 test_demo3
PASSED
test_case/test_demo_1.py::TestDemo2::test_demo4 test_demo4
PASSED
test_case/test_demo_2.py::TestDemo3::test_demo5 test_demo5
PASSED
test_case/test_demo_2.py::TestDemo3::test_demo6 test_demo6
PASSED
test_case/test_demo_2.py::TestDemo4::test_demo7 test_demo7
PASSED
test_case/test_demo_2.py::TestDemo4::test_demo8 test_demo8
PASSED

=================================================== 8 passed, 12 deselected in 0.21s ===================================================


我们会发现,当我们关闭fixture函数的自调用开关之后,
所有的fixture函数都没有被执行,
这是因为我们在pytest用例的参数列表中没有引用任何fixture;

相关文章

  • python_ui自动化之公共数据

    方案一 代码示例: 方案二 代码示例: conftest.py文件作用范围,当前包及其子包;conftest.py...

  • Day16大数据颠覆公共利益(30天训练营)

    思想实验——大数据会颠覆公共利益么? “无知之幕”与公共利益 什么是公共利益? ——它是我们对社会的共同期望。 但...

  • android高级知识汇总

    原文地址 公共技术点之 Android 动画基础公共技术点之 Java 动态代理公共技术点之依赖注入公共技术点之 ...

  • 2017年秋招Android面试的21个重难点

    Android 面试重难点 Android公共技术: 公共技术点之 Android 动画基础 公共技术点之 Jav...

  • 自动化测试之断言:TestNG断言类简述

    TestNG之HardAssert和SoftAssert 我们做自动化的时候经常把代码的重点放在逻辑层,或者数据层...

  • 数据产品及经理理解

    数据产品概念: 以数据为主的自动化产出的自动化产品形态。 Eg:每日数据产出,日志提取 数据产品的分类: 1、针对...

  • 程序设计

    1、数据基类 管理数据 2、基本信息 公共方法,公共UI 3、逻辑处理 4、界面处理 可分模块: 数据,UI,...

  • 公共数据缓存

    公共数据缓存调用 工具方法: 新建一个类,全局一个实例,暴露方法:getCacheDataByKey 1 先判断传...

  • Selenium自动化测试框架和个人见解

    Selenium自动化测试框架和个人见解 使用数据驱动和关键字驱动构建自动化测试框架 数据驱动 在自动化测试框架中...

  • 通过建立隧道链接内网数据库

    内网有两台主机,一台做公共服务,一台做数据库,其中公共服务有外网ip,而数据库服务没有。公共服务可以链接数据库,但...

网友评论

      本文标题:python_ui自动化之公共数据

      本文链接:https://www.haomeiwen.com/subject/ovmvxktx.html