#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
import pytest
__author__ = "Carp-Li"
__date__ = "2020/10/10"
class TestClassCase:
@pytest.mark.parametrize("phone,code,msg", [
("13250813191", "1234", "应该验证通过"),
("1325081319", "1234", "手机号长度不足11位,应该验证失败"),
("13250813191", "123", "验证码长度不足4位,应该验证失败"),
])
def test_param_case(self, phone, code, msg):
""" 1、基于pytest.mark.parametrize装饰器实现测试用例参数化 """
assert len(phone) == 11 and len(code) == 4, msg
# 2、使用fixture中的param参数,完成前置条件的参数化
@pytest.fixture(params=[
{"username": "admin", "scope": 1},
{"username": "user", "scope": 2}
], ids=["admin", "user"])
# 4、ids参数只是对应params里面参数的标识,如果不传,则回去变量名或者函数名+序号
# @加了ids的参数,pytest命令行执行用例时,可以使用-k id_name 来执行该条用例
def init_func(self, request):
# 3、只能通过request.param获取参数
self.scope = request.param['scope']
return self.scope
def test_fixture_param_case(self, init_func):
assert init_func == 1, "登录用户必须是管理员"
@pytest.fixture()
def get_auth(self, request):
return request.param['scope']
# indirect=True时,第一个参数就不再是变量名而是函数了
@pytest.mark.parametrize("get_auth", [
{"username": "admin", "scope": 1},
{"username": "user", "scope": 2}
], indirect=True)
def test_data_case(self, get_auth):
# 使用pytest.mark.parametrize+fixture实现前置条件的参数化
assert get_auth == 1, "登录用户必须是管理员"