5、将所有的框架综合运用:POM+DDT+pytest+allure

1、首先实现PO效果(页面代码 与测试代码分离)
2、选择合适的DDT框架(变量,文件(yaml,json,csv),pytest下的参数化)
2.1 python--yaml灵活,配置文件,数据文件 ;写接口建议用yaml的。
2.2 如果写功能,测试用例会写在excel表里,excel只能window下执行,csv文件可以在windows,linux执行。只要把excel转成csv.
2.3 写功能自动化脚本,用pytest下的参数化
3、写测试脚本,可以在原有的基础上修改。如果你一开始写的unittest的脚本 ,可以不改。pytest执行框架支持unittest脚本。
4、写测试脚本 ,使用pytest,allure框架
5、运行脚本,pytest,allure生成报告。
6、在jenkins中执行脚本。


1、在已有的PO代码中进行修改。
2、在测试文件中添加DDT的框架

  • test_data_soso.yaml
- selenium
- python
- DDT

  • 代码修改后:
from selenium import webdriver
import time
import unittest
import datetime,os
from ..pages import pages
# 1、加导入
from ddt import ddt,file_data

# 2、在类上加@ddt
@ddt
class TestBingSo(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        print("这是初始化开始测试")
        parent_abspath = os.path.abspath(os.path.dirname(os.getcwd()))
        driver_path =os.path.join(parent_abspath,"driver/chromedriver")
        cls.driver = webdriver.Chrome(executable_path=driver_path)
        cls.driver.get("https://cn.bing.com/?ensearch=1&FORM=BEHPTB")
        cls.driver.maximize_window()


    @classmethod
    def tearDownClass(cls):
        print("执行完成环境销毁")
        cls.driver.quit()

    # 3、在测试方法上加@file_data,通过yaml类型的文件导入数据
    @file_data("test_data_soso.yml")
    def test_soso(self,sosovalue):

        main_page =pages.MainPage(self.driver)
        assert 'Bing' in main_page.title_matches()
        # 4、变量值sosovalue在输入搜索值的地方替换
        main_page.enter_search_text(sosovalue)
        main_page.click_search_button()
        search_result_page = pages.SearchResultPage(self.driver)
        # 5、如果断言相同 ,修改断言为变量
        assert sosovalue in search_result_page.results_found()
        search_result_page.save_pic(str(datetime.datetime.now())+"_soso.png")


if __name__ == '__main__':
    unittest.main()

3、pytest,allure框架,DDT使用pytest.mark.parametrize读取yaml和csv文件两种方式实现,allure报告 中附加截图

import pytest
# 先导入pip install pytest
from selenium import webdriver
import datetime, os
from util.get_path import get_par_path
import allure
from fourday.pageobject.pages import pages
from util.read_yaml import get_yaml_data
from util.get_csvdata import get_csv_data


@allure.feature('打开浏览器')
@pytest.fixture(scope="module")
def driver(request):
    driver_path = os.path.join(get_par_path(), "driver/chromedriver")
    driver = webdriver.Chrome(executable_path=driver_path)

    def close_browser():
        driver.quit()

    # 无论执行正确和错误最终都执行关闭浏览器的方法
    request.addfinalizer(close_browser)
    return driver


class TestBingSo():
    @allure.feature("打开bing首页")
    @pytest.fixture(scope="module",autouse=True)
    def open_bingpage(self, driver):
        with allure.step("step 1:打开浏览器,输入必应的地址"):
            driver.get("https://cn.bing.com/?ensearch=1&FORM=BEHPTB")
            driver.maximize_window()
            driver.implicitly_wait(10)

    test_data =get_yaml_data("test_data_soso.yml")

    @allure.step("这是初始化数据")
    @pytest.fixture()
    def get_data(self,request):
        value=request.param
        return value

    @pytest.mark.parametrize("get_data",test_data,indirect=True)
    @allure.feature("必应的搜索功能")
    @allure.story("验证不同的搜索信息,返回的结果是否正确")
    def test_soso(self,driver,get_data):
        with allure.step("step 2:加载主页页面,加载成功"):
            main_page = pages.MainPage(driver)
            assert 'Bing' in main_page.title_matches()
        with allure.step("step 3:输入搜索信息,点击搜索"):
            main_page.enter_search_text(get_data)
            main_page.click_search_button()
        with allure.step("step 4:加载搜索结果页"):
            search_result_page = pages.SearchResultPage(driver)
        with allure.step("step 5:验证结果成功"):
            assert get_data in search_result_page.results_found()
        with allure.step("step 6:截图"):

            file_name =str(datetime.datetime.now()) + "_soso.png"
            search_result_page.save_pic(file_name)
            allure.attach.file(file_name,"搜索结果成功截图",attachment_type=allure.attachment_type.PNG)

    test_login_data = get_csv_data("test_data1.csv")


    @pytest.mark.parametrize("get_data", test_login_data, indirect=True)
    @allure.feature("登陆功能")
    @allure.story("验证不同的用户登陆,返回的结果是否正确")
    def test_login(self,driver,get_data):
        username = get_data[0]
        password = get_data[1]
        print("用户名: %s " % username)
        print("密码: %s " % password)
        with allure.step("step 888:加载主页页面,加载成功"):
            main_page = pages.MainPage(driver)
            assert 'Bing' in main_page.title_matches()

if __name__ == '__main__':
    pytest.main(['-s','test_case_pytest.py'])

  • 其他相关文件
    get_csv_data.py
import csv

def get_csv_data(file_name):
    rows=[]
    with open(file_name,'r') as f:
        reader = csv.reader(f)
        next(reader,None)
        for row in reader:
            rows.append(row)
        return rows

get_path.py

import os

def get_par_path():
    root_path = os.path.abspath(os.path.dirname(__file__)).split('util')[0]
    # 返回的就是根路径
    return root_path

read_yaml.py

import yaml

def get_yaml_data(yaml_file):
    # 打开yaml文件
    with open(yaml_file, 'r', encoding="utf-8") as file:
        file_data = file.read()


    data = yaml.load(file_data)
    return data

你可能感兴趣的:(5、将所有的框架综合运用:POM+DDT+pytest+allure)