简体   繁体   English

在python unittest中创建模拟对象

[英]Creating mock object within python unittest

I am completely new to unittest in Python, so sorry if this is a noob question. 我对使用Python进行单元测试是完全陌生的,如果这是一个菜鸟问题,对不起。

I have a Customer class: 我有一个客户类:

class Customer(object):

    def __init__(self, name, clubMember, taxExempt):
        self.name = name
        self.clubMember = clubMember
        self.taxExempt = taxExempt

    def isClubMember(self):
        if (self.clubMember == True):
            return True
        else:
            return False

    def isTaxExempt(self):
        if (self.taxExempt == True):
            return True
        else:
            return False

And I want to test it with a kind of mock Customer with: 我想用一种模拟客户来测试它:

import unittest
import Customer 

class TestCustomer(unittest.TestCase):
    def setUp(self):
        self.customer = Customer("John Doe", True, False)

    def test_customer_member_status(self):
        self.assertTrue(self.customer.isClubMember(), 'incorrect member status')

    def test_customer_tax_exemption(self):
        self.assertFalse(self.customer.isTaxExempt(), 'incorrect tax status')

    def tearDown(self):
        self.customer.dispose()

However, I receive the following error: 但是,我收到以下错误:

self.customer = Customer("John Doe", True, False)
TypeError: 'module' object is not callable

I've tried to search many different websites, but I cannot find the correct format to create the mock Customer. 我尝试搜索许多不同的网站,但找不到正确的格式来创建模拟客户。 In addition, I'm not sure how to create multiple mock Customers to test another example, such as Customer("Jane Doe", False, True) etc. 此外,我不确定如何创建多个模拟客户来测试另一个示例,例如Customer(“ Jane Doe”,False,True)等。

I guess your module name is also Customer and when you are doing import Customer , it is importing the module and not the class. 我猜您的模块名称也是Customer ,当您import Customer ,它是在导入模块而不是类。

Do this instead 改为这样做

from Customer import Customer 

in test moudle. 在测试模块中。

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

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