繁体   English   中英

pytest 基于从自定义类获得的数据进行参数化

[英]pytest parameterization based on data obtained from custom classes

如何使用从其他自定义类获得的数据参数化测试 function? 在文档中,我只看到作为全局元组列表给出的 static 数据的参数化示例。

就我而言,我正在测试我写的 function,它可以在另一个图像中找到一个图像。 它看起来像这样:

def search_for_image_in_image(screenshot_path, detectable_path):
    """
    Uses template matching algorithm to detect an image within an image
    :param screenshot_path: Path to the screenshot to search
    :param detectable_path: Path to the detectable to search for
    :return: tuple containing:
        (bool) - Whether or not the detectable was found within the screenshot, using
            the given epsilon
        (float) - Maximum value the algorithm found
        (list) - x and y position in the screenshot where the maximum value is located. 
            Or in other words, where the algorithm thinks the top left of the detectable
            is most likely to be (if it is there at all)  

因此,我设置了一些示例数据进行测试:

tests\
    sample data\
        images_to_search_for\
            moe.png
            larry.png
            curly.png
        images_to_search
            screenshot_01.png
            screenshot_02.png
        expected_results.csv

我手动创建了 csv 文件,如下所示:

screenshot_name,moe,larry,curly
screenshot_01,True,False,True       
screenshot_02,False,False,False

我可以创建类或函数来加载此示例数据,但我不明白如何将它传递给我的测试方法。

这是我的测试代码的骨架:

import pytest
from image_detection import search_for_image_in_image

class DataLoader(object):
    def __init__(self):
        # Load all the data up
        pass

    def get_screenshot_paths(self):
        """
        :return:  (list of string) paths to all the images to search
        """
        pass

    def get_detectable_paths(self):
        """
        :return: (list of string) paths to all the images to search for
        """
        pass

    def is_expected_to_be_found(self, screenshot_name, detectable_name):
        """
        :param screenshot_name:
        :param detectable_name:
        :return: Whether or not the detectable is expected to be found in the screenshot
        """
        pass


@pytest.mark.parametrize("screenshot_path,detectable_path,expected_result", ???? )
def test_image_searching(screenshot_path, detectable_path, expected_result):
    actual_result, _, _  = search_for_image_in_image(screenshot_path, detectable_path)

我把什么放在我有“???”的地方或者我是否以其他方式谈论它?

我明白你的意思了。 我从您的问题中了解到,您想根据其他函数的返回值对测试方法进行参数化,这是您在 pytest 文档中看不到的。

为此,您必须使用 pytest 挂钩函数(pytest_generate_tests)来参数化测试方法。

    import pytest

    def pytest_generate_tests(metafunc):
        """
        This method will call before the execution of all the tests methods.
        """
        #   your logic here, to obtain the value(list data type) from any other      custom classes.
        #   for e.g:-
        data_loader = DataLoader()
        images_path  = data_loader.get_screenshot_paths()
        images_path_1 = data_loader.get_detectable_paths()
        metafunc.parametrize("first_path","seconds_path", images_path, images_path_1)
        # now, whenever this method will called by test methods, it will automatically parametrize with the above values. 



    def test_1(first_path, second_path):
        """
        Your tests method here
        """

我希望你能找到你的答案。 参考( https://docs.pytest.org/en/latest/parametrize.html

您需要创建一个 function 来处理数据

def data_provider():
    data_loader = DataLoader()
    yield pytest.param(data_loader.get_screenshot_paths(), data_loader.get_detectable_paths(), data_loader.is_expected_to_be_found('name_a', 'name_b'))

@pytest.mark.parametrize('screenshot_path, detectable_path, expected_result', data_provider())
def test_image_searching(self, screenshot_path, detectable_path, expected_result):
    actual_result, _, _  = search_for_image_in_image(screenshot_path, detectable_path)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM