为类的 __init__ 方法编写单元测试
我对单元测试和编写/使用异常非常陌生。我目前正在付出巨大的努力来学习最佳实践并将其集成到我的项目中。作为对我一直在阅读的一些内容的测试,我编写了一个简单的合同模块。下面是合约类的 init,它有几个相互依赖的参数。
我将/应该如何根据 init 方法的参数依赖关系编写测试。
提前致谢!
def __init__(self, code, description ,contract_type,
start_date ,end_date ,reminder_date,
customer=None, isgroup=False, vendor=None,
discount_perc=None):
contract_types = ['item','vendor']
self.code = code
self.description = description
self.contract_type = contract_type
self.start_date = start_date
self.end_date = end_date
self.reminder_date = reminder_date
if contract_type not in contract_types:
raise AttributeError("Valid contract types are 'item' & 'vendor'")
if isgroup:
if customer:
raise AttributeError("Group contracts should not have 'customer' passed in")
self.is_group_contract = True
else:
if customer:
self.add_customer(customer)
else:
raise AttributeError('Customer required for non group contracts.')
if contract_type == 'vendor':
if vendor and discount_perc:
self.vendor = vendor
self.discount_perc = discount_perc
else:
if not vendor:
raise AttributeError('Vendor contracts require vendor to be passed in')
if not discount_perc:
raise AttributeError('Vendor contracts require discount_perc(Decimal)')
如果此类问题不适合 SO,我最好去哪里?
I am very new to unit testing and writing/using exceptions. I am currently making a huge effort to learning about best practices and integrating them into my projects. As a test of some things I have been reading about I wrote a simple Contracts module. Below is the init of the contract class which has several arguments that depend on each other.
How would/should I write a test for the init method based on its argument dependencies.
Thanks in advance!
def __init__(self, code, description ,contract_type,
start_date ,end_date ,reminder_date,
customer=None, isgroup=False, vendor=None,
discount_perc=None):
contract_types = ['item','vendor']
self.code = code
self.description = description
self.contract_type = contract_type
self.start_date = start_date
self.end_date = end_date
self.reminder_date = reminder_date
if contract_type not in contract_types:
raise AttributeError("Valid contract types are 'item' & 'vendor'")
if isgroup:
if customer:
raise AttributeError("Group contracts should not have 'customer' passed in")
self.is_group_contract = True
else:
if customer:
self.add_customer(customer)
else:
raise AttributeError('Customer required for non group contracts.')
if contract_type == 'vendor':
if vendor and discount_perc:
self.vendor = vendor
self.discount_perc = discount_perc
else:
if not vendor:
raise AttributeError('Vendor contracts require vendor to be passed in')
if not discount_perc:
raise AttributeError('Vendor contracts require discount_perc(Decimal)')
If this type of question isn't a good fit for SO, where might I be better of going?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会像任何其他(不是类或静态)方法一样对待 __init__ – 根据各种输入组合测试预期输出。但除此之外,我还会测试它是否返回(或不返回,具体取决于您的要求)单例对象。
然而,人们可能更愿意将单例测试提取为与 __new__ 相关的测试用例。
最终,您将进行以下测试:
另一个提示:将
contract_types = ['item','vendor']
提取到类属性将有助于业务逻辑测试组织。I'd treat
__init__
similar to any other (not class- or static-) method – test expected output based on various input combinations. But in addition to that, I'd also test it for returning (or not returning, depending on the requirements you have) singleton object.However one may prefer to extract singleton tests as the
__new__
-related test cases.Eventually you will have tests for:
Another tip: extracting
contract_types = ['item','vendor']
to the class attribute will help in business logic tests organization.