Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 基于从自定义类获得的数据的pytest参数化_Python_Unit Testing_Pytest - Fatal编程技术网

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

Python 基于从自定义类获得的数据的pytest参数化,python,unit-testing,pytest,Python,Unit Testing,Pytest,如何使用从其他自定义类获得的数据参数化测试函数?在文档中,我只看到对静态数据进行参数化的示例,这些示例是作为元组的全局列表给出的 在我的例子中,我正在测试我编写的一个函数,该函数在另一个图像中查找图像。 看起来是这样的: def search_for_image_in_image(screenshot_path, detectable_path): """ Uses template matching algorithm to detect an image within an

如何使用从其他自定义类获得的数据参数化测试函数?在文档中,我只看到对静态数据进行参数化的示例,这些示例是作为元组的全局列表给出的

在我的例子中,我正在测试我编写的一个函数,该函数在另一个图像中查找图像。 看起来是这样的:

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
        """

我希望你能找到答案。reference()

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

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)