美文网首页
47. parametrize + YAML案例

47. parametrize + YAML案例

作者: 薛东弗斯 | 来源:发表于2024-03-22 07:07 被阅读0次
project > paramertize > test_parameterize.py
project > paramertize > conftest.py
project > data > data.yaml
project > utils > read.py
project > utils > get_file_path.py
# test_parameterize.py
from time import sleep
import pytest

# 单参数,单次循环
# @pytest.mark.parametrize("key",["value"])
# def test_parametrize01(key):
#     print(key)

# 单参数,多次循环。数组里面有几组数据,循环几次
# @pytest.mark.parametrize("test", ['接口自动化', 'UI自动化', '性能测试'])
# def test_parametrize02(test):
#     assert test == "接口自动化"
from selenium.webdriver.common.by import By
from utils.read import read_yaml

@pytest.mark.parametrize("key", read_yaml()['skill'])
def test_baidu(driver, key):
    driver.get("https://www.baidu.com/")
    driver.find_element(By.ID, 'kw').send_keys(key)
    driver.find_element(By.ID, 'su').click()
    sleep(1)

if __name__=="__main__":
    pytest.main()
# conftest.py
import pytest
from selenium import webdriver
from utils.get_file_path import get_screen_shot_path


@pytest.fixture(scope="session")
def driver():
    global driver
    driver = webdriver.Chrome()
    driver.maximize_window()
    print("打开浏览器")
    yield driver
    print("关闭浏览器")
    driver.close()
    driver.quit()
# data.yaml
userinfo:
  username: admin
  password: 123456

skill:
  - 接口自动化
  - UI自动化
  - 性能测试

userinfos:
  - username: admin
    password: 123456
  - username: test01
    password: 123456

userinfo_list:
  - [ admin,123456 ]
  - [ test01,123456 ]

skills:
  - - 接口自动化
    - UI自动化
    - 性能测试

skills2:
  - [ 接口自动化, UI自动化, 性能测试 ]


#项目
register_ok:
  mobile: 13800001112
  password: 123456

register_failed:
  mobile: 13800001112
  password: 123456

register_exist:
  mobile: 13017744356

user_login:
  - mobile: ''
    password: 123456
  - mobile: 13800001111
    password: ''
  - mobile: 13800001111
    password: 123451
  - mobile: 13800001111
    password: 123456

user_address:
  province: 天津市
  city: 天津城区
  district: 和平区
  username: 老白
  useraddress: 人民广场100
  mobile: 13800001111
# read.py
import configparser

import yaml
from utils.get_file_path import get_yaml_path, get_ini_path

path = get_yaml_path()
ini_path = get_ini_path()


def read_yaml():
    with open(path, encoding="utf8") as f:
        data = yaml.safe_load(f)
        return data


def read_ini():
    config = configparser.ConfigParser()
    config.read(ini_path, encoding='utf8')
    return config


if __name__ == '__main__':
    print(read_yaml())
    # print(read_ini()['mysql']['HOST'])
# get_file_path.py
import os
import time


def get_report_path():
    path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "allure-report/export",
                        'prometheusData.txt')
    return path


def get_screen_shot_path():
    file_name = "截图{}.png".format(time.strftime("%Y-%m-%d_%H-%M-%S"))
    path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "file", file_name)
    return path


def get_logo_path():
    path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "file", "logo.jpg")
    return path


def download_file_path():
    path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "file")
    return path


def get_yaml_path():
    path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "data", "data.yaml")
    return path


def get_ini_path():
    path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "config", "settings.ini")
    return path


def get_log_path():
    path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "log")
    return path


if __name__ == '__main__':
    print(get_report_path())

test_parametrize_2.py

from time import sleep
import pytest
from selenium.webdriver.common.by import By
from utils.read import read_yaml

heros = [("安琪拉", "我的小熊去哪里了"), ("后裔", "周日被我射熄火了")]

# 多参数多次循环
@pytest.mark.parametrize("hero,word", heros)
def test_parametrize02(hero, word):
    print("{}的台词是{}".format(hero, word))


@pytest.mark.parametrize("key,value", [["安琪拉", "拉"], ["后裔", "后"]])
def test_parametrize(key, value):
    assert value in key

@pytest.mark.parametrize("username,password", read_yaml()['userinfo_list'])
def test_login(driver, username,password):
    driver.get("http://sellshop.5istudy.online/sell/user/login_page")
    driver.find_element(By.ID, 'username').send_keys(username)
    driver.find_element(By.ID, 'password').send_keys(password)
    driver.find_element(By.CSS_SELECTOR, '#login > form > p.login.button > input[type=submit]').click()
    if username == 'admin':
        text = driver.find_element(By.CSS_SELECTOR,"body > div > div > div > div > strong").text
        assert text == "用户不存在"
    sleep(2)

相关文章

网友评论

      本文标题:47. parametrize + YAML案例

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