pytest:fixture 之返回值

pytest.fixture 返回值

  • 默认返回None
  • 返回需要的值
  1. 通过pytest.fixture标记函数 @pytest.fixture(scope='module')
    对session只运行一次,所有testcase均链接同一数据库时,则可以设置scope等于 'module'
    Note:因为当前我的登录用户是唯一的,所以我也设置为moduel模式
样例: test_login.py 
目的:获取需要的token
@pytest.fixture(scope='module')
def login():
         url = "你需要请求的url"
        querystring = “请求参数”
        headers = “请求头文件”
       r = requests.post(url, data, headers)
       value = re.findall(r'

2.测试方法中调用返回值
在样例的函数中设置了fixture,在测试方法中需要token,那么就把login这个函数,传递给类方法

样例: test_login.py 
目的:使用token的值【fixture部分可以单独作为一个模块,后续其他模块直接调用即可,在本文中,fixture和测试方法在同一文件】

class TestClass:
    def test_get_current_user_info(self, login):
        url = "你需要请求的url"
        querystring = “请求参数”
        headers = “请求头文件”
        result = respons.http_request(interface_url=url, interface_param=querystring, headerdata=headers,
                                 request_type='get')
        m = json.loads(result['data'])
        assert m['status'] ==0


3.完整的test_login.py内容

# !__author__ =='wuwa'
# ! -*- utf-8 -*-
import json
import re

import pytest
import requests

from libarry.httprequests import requests_interface

respons = requests_interface()


@pytest.fixture(scope='module')
def login():
"""
获取token
"""
     url = "你需要请求的url"
     querystring = “请求参数”
      headers = “请求头文件”
 
    r = requests.post(url, data, headers)
    value = re.findall(r'
20180314.png

你可能感兴趣的:(pytest:fixture 之返回值)