首页 > 解决方案 > Pytest - 如何存根成员变量

问题描述

我很抱歉——这是我第一次尝试使用 pytest 或任何 python 测试库,但我已经做了少量的 JUnit,所以对这些原理有点熟悉。

基本上,我想要测试的类有几个我想要存根的成员变量。具体来说,我只需要一些客户详细信息。我在 OrderController 类的类变量“orders”中访问它(以购买 id 作为键,以订单对象作为值的字典列表)。当我得到这个订单对象时,我想访问由他们的姓名和地址组成的客户属性——这个地址属性是另一个成员变量。

下面是 address_label.py 模块(对于这些评论,我很抱歉 - 它适用于大学)

"""
--------------------------------------------------------------------------------------------
title           : address_label.py
description     : Formats order data for creation of address labels in the pdf.py module.
python_version  : 3.7.9
--------------------------------------------------------------------------------------------
"""

from .order_controller import OrderController
from .pdf import Pdf


class AddressLabel:
    """
    A class for formatting data to be input into address label pdf.

    ...

    Attributes
    ----------
    order_controller : OrderController
        member variable to access order instances
    pdf : Pdf
        member variable to invoke writing of pdf

    Methods
    -------
    create_address_label(orders_selected):
        Create strings to be output in pdf
    """

    def __init__(self):
        """
        Constructs all the necessary attributes for the AddressLabel object.
        """
        self._order_controller = OrderController(None)
        self._pdf = Pdf()

    def create_address_label(self, orders_selected):
        """
            For each of the orders selected with checkboxes will find the data for that order
            and format is suitable for the pdf module.
       
            Parameters:
                orders_selected: an array of the row data from each row checked with a checkbox
                (each item is a string).
        """
        for index, order in enumerate(orders_selected):
            order_id = int(order[0]) - 1
            order_obj = self._order_controller.orders['order_' + order[0]]

            address = [order_obj.customer.first_name + ' ' + order_obj.customer.last_name,
                       order_obj.customer.address.line_one, order_obj.customer.address.city]

            self._pdf.write_address_label(address, order_id, index)

            return address, order_id, index

这是迄今为止我对 test_address_label.py 所做的,但我注意到它仍在联系主 OrderController 类,因此失败了 - 我该如何阻止它?

import pytest
from main.business_logic.address_label import AddressLabel


class Address:
    def __init__(self, line_one, line_two, city):
        self.line_one = line_one
        self.line_two = line_two
        self.city = city


class Customer:
    def __init__(self, address, first_name, last_name):
        self.address = address
        self.first_name = first_name
        self.last_name = last_name


class Order:
    def __init__(self, customer):
        self.customer = customer


class OrderController:
    orders = {
        'order_1': Order(customer=setup_customer())
    }

    def __init__(self, x):
        pass
    
    @staticmethod
    def setup_customer():
        def setup_address():
            return Address(line_one='Test Line One',
                                line_two='Test Line Two', city='Test City')

        address = setup_address()
        return Customer(address=address, first_name='Test First Name', last_name='Test Last Name')

@pytest.fixture
def _order_controller():
    return OrderController()

def test_address_label(_order_controller):
    address_label = AddressLabel()
    orders_selected = [['1', 'Test Name', '2021-03-12', 'Status', '£200']]
    scenario_one_address = ['Test First Name Test Last Name', 'Test Line One', 'Test City'] 

    address_label_contents = address_label.create_address_label(
        orders_selected)
    assert address_label_contents == (scenario_one_address, 1, 0)

无论如何,如果有人有任何好的资源可以从中学习,那就太好了-我已经阅读了很多教程,但是它们都使用了不适用于我的很多用例的基本示例...

先感谢您!

标签: pythonpython-3.xpytest

解决方案


推荐阅读