1. 程式人生 > 其它 >Pytest @pytest.mark.parametrize示例

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
鐘聲敲響了日落,柏油路躍過山坡,一直通向北方的是我們想象,長大後也未曾經過~