วิธีจับภาพหน้าจอกรณีทดสอบล้มเหลวด้วย PyTest

ขณะนี้ฉันกำลังใช้โซลูชันต่อไปนี้เพื่อจับภาพหน้าจอในตอนท้ายของทุกฟังก์ชันการทดสอบด้วย PyTest ฉันจะแน่ใจได้อย่างไรว่าจะมีการจับภาพหน้าจอเฉพาะในกรณีที่การทดสอบล้มเหลวเท่านั้น นี่เป็นคำถามเกี่ยวกับกลไกของ PyTest คำถามนี้ไม่เกี่ยวกับซีลีเนียมหรือแอปเปียม

ฉันพบคำถามที่คล้ายกันที่นี่ใน Stackoverflow แต่ไม่เหมือนกันทุกประการ วิธีแก้ไขที่ให้ไว้สำหรับคำถามอื่นๆ เหล่านั้นไม่ตอบคำถามของฉัน เนื่องจากการถ่ายภาพหน้าจอเกี่ยวกับความล้มเหลวในการทดสอบด้วย PyTest เป็นปัญหาที่พบบ่อย ฉันจึงเชื่อว่าสมควรได้รับคำตอบที่แยกจากกันและค่อนข้างเฉพาะเจาะจง

@pytest.fixture(scope="function", autouse=True)
def take_screenshot(self, appium_driver):
    yield
    time.sleep(1)
    current_filename_clean = os.path.basename(__file__).replace("test_", "").replace(".py", "")
    current_test_name = os.environ.get("PYTEST_CURRENT_TEST").split(":")[-1].split(" ")[0].replace("test_", "")
    appium_driver.get_screenshot_as_file(
        f'test_reports/{current_filename_clean}_android_{current_test_name}_{datetime.today().strftime("%Y-%m-%d")}.png')

person Ostap Didenko    schedule 13.02.2020    source แหล่งที่มา
comment
นี่เป็นหัวข้อยอดนิยมที่มีการกล่าวถึงหลายครั้งในเอกสาร ตัวอย่างฟิกซ์เจอร์: Making ข้อมูลผลการทดสอบที่มีอยู่ในฟิกซ์เจอร์ ตัวอย่างเบ็ด: รายงานการทดสอบหลังการประมวลผล / ความล้มเหลว   -  person hoefling    schedule 13.02.2020
comment
ฉันพบบทความเหล่านั้น แต่วิธีแก้ปัญหานั้นไม่ได้ตรงไปตรงมาสำหรับปัญหาของฉัน ฉันเชื่อว่า เนื่องจากนี่เป็นปัญหาที่ค่อนข้างธรรมดา จึงเป็นการดีกว่าที่จะทุ่มเทให้กับคำตอบที่เฉพาะเจาะจงและตรงประเด็น รวมถึงแค็ตตาล็อกแนวทางปฏิบัติที่ดีที่สุด   -  person Ostap Didenko    schedule 18.02.2020


คำตอบ (2)


นี่คือโซลูชันเต็มรูปแบบสำหรับไฟล์ conftest.py ของคุณสำหรับการเรียกใช้งานแบบไม่มีหัวในคอนเทนเนอร์นักเทียบท่า:

import time
from datetime import datetime    
import pytest
import os
from selenium import webdriver as selenium_webdriver
from selenium.webdriver.chrome.options import Options

# set up webdriver fixture
@pytest.fixture(scope='session')
def selenium_driver(request):
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')

    driver = selenium_webdriver.Chrome(options=chrome_options)
    driver.set_window_size(1920, 1080)
    driver.maximize_window()
    driver.implicitly_wait(5)

    yield driver
    driver.quit()

# set up a hook to be able to check if a test has failed
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    # execute all other hooks to obtain the report object
    outcome = yield
    rep = outcome.get_result()

    # set a report attribute for each phase of a call, which can
    # be "setup", "call", "teardown"

    setattr(item, "rep_" + rep.when, rep)

# check if a test has failed
@pytest.fixture(scope="function", autouse=True)
def test_failed_check(request):
    yield
    # request.node is an "item" because we use the default
    # "function" scope
    if request.node.rep_setup.failed:
        print("setting up a test failed!", request.node.nodeid)
    elif request.node.rep_setup.passed:
        if request.node.rep_call.failed:
            driver = request.node.funcargs['selenium_driver']
            take_screenshot(driver, request.node.nodeid)
            print("executing test failed", request.node.nodeid)

# make a screenshot with a name of the test, date and time
def take_screenshot(driver, nodeid):
    time.sleep(1)
    file_name = f'{nodeid}_{datetime.today().strftime("%Y-%m-%d_%H:%M")}.png'.replace("/","_").replace("::","__")
    driver.save_screenshot(file_name)
person Ostap Didenko    schedule 10.05.2020

มีอีกวิธีหนึ่งที่คล้ายกับวิธีของ @Ostap: การใช้ pytest_runtest_makereport (เอกสาร, การอ้างอิง API ) ความสามารถในการประมวลผลภายหลัง ง่ายกว่าเล็กน้อย:

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    rep = outcome.get_result()
    if rep.when == 'call' and rep.failed:
        now = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
        driver.save_screenshot(f".\\Screenshots\\fail_{now}.png")
person Mate Mrše    schedule 20.04.2021