为类的 __init__ 方法编写单元测试

发布于 2025-01-04 21:33:48 字数 1505 浏览 1 评论 0原文

我对单元测试和编写/使用异常非常陌生。我目前正在付出巨大的努力来学习最佳实践并将其集成到我的项目中。作为对我一直在阅读的一些内容的测试,我编写了一个简单的合同模块。下面是合约类的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东北女汉子 2025-01-11 21:33:48

我会像任何其他(不是类或静态)方法一样对待 __init__ – 根据各种输入组合测试预期输出。但除此之外,我还会测试它是否返回(或不返回,具体取决于您的要求)单例对象。
然而,人们可能更愿意将单例测试提取为与 __new__ 相关的测试用例。

最终,您将进行以下测试:

  1. 无效参数的类型处理(空/非空字符串、整数、元组、字典等)。
  2. 无效的参数组合处理(在您的情况下它会引发异常)。
  3. 可选参数存在/不存在处理(默认值有效,自定义值也有效,等等)。
  4. 有效参数组合处理(正流有效)。
  5. 结果对象的属性存在/不存在及其值(大多数情况下您在其他方法中依赖它们)。
  6. 生成的对象是单例(或不是)。
  7. ???

另一个提示:将 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:

  1. Invalid arguments' types handling (empty/not empty strings, integers, tuples, dicts, etc.).
  2. Invalid arguments combinations handling (in you case it's risen exceptions).
  3. Optional arguments presence/absence handling (default values work, custom ones do too, etc.).
  4. Valid arguments combinations handling (positive flow works).
  5. Resulting object's attributes presence/absence and their values (most certainly you rely on them in other methods).
  6. Resulting object being singleton (or not).
  7. ???

Another tip: extracting contract_types = ['item','vendor'] to the class attribute will help in business logic tests organization.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文