Pytest如何存根成员变量

2024-09-27 19:22:53 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

下面是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)

在任何情况下,如果有任何人有任何好的资源来学习这一点,那将是伟大的-我读了很多教程,但他们都使用这样的基本例子,不适用于我的许多用例

提前谢谢你


Tags: thenametestselfpdfaddressdefline

热门问题