Pytest @pytest.mark.parametrize示例

示例:

# -*-coding: utf-8 -*-

from selenium import webdriver
import pytest
import time
search_list = ['小米','小米手机','小米10s','红米','耳机']

def setup_module():
    global driver
    driver = webdriver.Chrome()
    driver.get('https://www.mi.com/')
    time.sleep(2)
def teardown_module():
    global driver
    driver.quit()
@pytest.mark.parametrize('item',search_list)
def test_search_keywords(item):
    global driver
    driver.find_element_by_css_selector('#search').clear()
    driver.find_element_by_css_selector('#search').send_keys(item+'\n')
    time.sleep(2)
    elements = driver.find_elements_by_css_selector('.goods-list .title')
    good_list = [element.text for element in elements]
    for i in good_list:
        assert item in i


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

控制台输出:

============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: E:\PycharmProjects\Pytest-Webui-songqin
plugins: allure-pytest-2.8.40, html-3.1.1, metadata-1.11.0
collected 5 items

test_scripts03.py FFFFF

================================== FAILURES ===================================
_____________________ test_search_keywords[\u5c0f\u7c73] ______________________

item = '小米'

    @pytest.mark.parametrize('item',search_list)
    def test_search_keywords(item):
        global driver
        driver.find_element_by_css_selector('#search').clear()
        driver.find_element_by_css_selector('#search').send_keys(item+'\n')
        time.sleep(2)
        elements = driver.find_elements_by_css_selector('.goods-list .title')
        good_list = [element.text for element in elements]
        for i in good_list:
>           assert item in i
E           AssertionError: assert '小米' in 'Xiaomi MIX 4 12GB+512GB'

test_scripts03.py:25: AssertionError
_______________ test_search_keywords[\u5c0f\u7c73\u624b\u673a] ________________

item = '小米手机'

    @pytest.mark.parametrize('item',search_list)
    def test_search_keywords(item):
        global driver
        driver.find_element_by_css_selector('#search').clear()
        driver.find_element_by_css_selector('#search').send_keys(item+'\n')
        time.sleep(2)
        elements = driver.find_elements_by_css_selector('.goods-list .title')
        good_list = [element.text for element in elements]
        for i in good_list:
>           assert item in i
E           AssertionError: assert '小米手机' in '小米11 8GB+128GB'

test_scripts03.py:25: AssertionError
____________________ test_search_keywords[\u5c0f\u7c7310s] ____________________

item = '小米10s'

    @pytest.mark.parametrize('item',search_list)
    def test_search_keywords(item):
        global driver
        driver.find_element_by_css_selector('#search').clear()
        driver.find_element_by_css_selector('#search').send_keys(item+'\n')
        time.sleep(2)
        elements = driver.find_elements_by_css_selector('.goods-list .title')
        good_list = [element.text for element in elements]
        for i in good_list:
>           assert item in i
E           AssertionError: assert '小米10s' in '小米10S 8GB+128GB'

test_scripts03.py:25: AssertionError
_____________________ test_search_keywords[\u7ea2\u7c73] ______________________

item = '红米'

    @pytest.mark.parametrize('item',search_list)
    def test_search_keywords(item):
        global driver
        driver.find_element_by_css_selector('#search').clear()
        driver.find_element_by_css_selector('#search').send_keys(item+'\n')
        time.sleep(2)
        elements = driver.find_elements_by_css_selector('.goods-list .title')
        good_list = [element.text for element in elements]
        for i in good_list:
>           assert item in i
E           AssertionError: assert '红米' in 'Redmi Note 10 Pro 6GB+128GB'

test_scripts03.py:25: AssertionError
_____________________ test_search_keywords[\u8033\u673a] ______________________

item = '耳机'

    @pytest.mark.parametrize('item',search_list)
    def test_search_keywords(item):
        global driver
        driver.find_element_by_css_selector('#search').clear()
        driver.find_element_by_css_selector('#search').send_keys(item+'\n')
        time.sleep(2)
        elements = driver.find_elements_by_css_selector('.goods-list .title')
        good_list = [element.text for element in elements]
        for i in good_list:
>           assert item in i
E           AssertionError: assert '耳机' in 'Redmi AirDots 3 Pro 曜石黑'

test_scripts03.py:25: AssertionError
=========================== short test summary info ===========================
FAILED test_scripts03.py::test_search_keywords[\u5c0f\u7c73] - AssertionError...
FAILED test_scripts03.py::test_search_keywords[\u5c0f\u7c73\u624b\u673a] - As...
FAILED test_scripts03.py::test_search_keywords[\u5c0f\u7c7310s] - AssertionEr...
FAILED test_scripts03.py::test_search_keywords[\u7ea2\u7c73] - AssertionError...
FAILED test_scripts03.py::test_search_keywords[\u8033\u673a] - AssertionError...
============================= 5 failed in 40.06s ==============================

Process finished with exit code 0

 

posted @ 2021-08-30 21:55  Avicii_2018  阅读(56)  评论(0编辑  收藏  举报