简体   繁体   English

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

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

How do I parameterize a test function using data that I obtain from other custom classes?如何使用从其他自定义类获得的数据参数化测试 function? In the documentation, I only see examples of parameterizing on static data given as a global list of tuples.在文档中,我只看到作为全局元组列表给出的 static 数据的参数化示例。

In my case, I am testing a function I wrote that finds a image within another image.就我而言,我正在测试我写的 function,它可以在另一个图像中找到一个图像。 It looks like so:它看起来像这样:

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)  

So, I set up some sample data to test with:因此,我设置了一些示例数据进行测试:

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

I manually created the csv file like so:我手动创建了 csv 文件,如下所示:

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

I can create classes or functions to load this sample data, but I don't understand how I am to pass it to my test method.我可以创建类或函数来加载此示例数据,但我不明白如何将它传递给我的测试方法。

Here is the skeleton of my test code:这是我的测试代码的骨架:

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)

What do I put where I have "????"我把什么放在我有“???”的地方Or do I go about it some other way?或者我是否以其他方式谈论它?

I got your point.我明白你的意思了。 What I understand from your question is you want to parametrize the test methods from the return value of other functions, that you are unable to see in the pytest docs.我从您的问题中了解到,您想根据其他函数的返回值对测试方法进行参数化,这是您在 pytest 文档中看不到的。

For that, You have to use pytest hook function(pytest_generate_tests) to parametrize the tests methods.为此,您必须使用 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
        """

I hope you find your answer.我希望你能找到你的答案。 Refer( https://docs.pytest.org/en/latest/parametrize.html )参考( https://docs.pytest.org/en/latest/parametrize.html

You need to create a function to handle the data您需要创建一个 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