gpt4 book ai didi

python - Py.test mixin类无法访问 `self`

转载 作者:行者123 更新时间:2023-11-30 22:50:08 24 4
gpt4 key购买 nike

我正在尝试为一组共享测试创建一个 mixin。每当我想要运行这些通用测试时,我都希望能够从 mixin 继承。

这是我的一些混音:

class CommonRuleWhenTestsMixin(TestCase):

def test_returns_false_if_rule_inactive(self):
self.rule.active = False

assert not self.rule.when(self.sim)

这是我使用 mixin 时的情况:

class TestWhen(CommonRuleWhenTestsMixin):

def setUp(self):
self.customer = mommy.make(Customer)
self.rule = mommy.make(
UsageRule,
customer=self.customer,
max_recharges_per_month=2
)
self.sim = mommy.make(
Sim,
msisdn='0821234567',
customer=self.customer
)

assert self.rule.when(self.sim)

def test_returns_false_if_airtime_max_recharges_exceeded(self):
self.rule.recharge_type = AIRTIME
mommy.make(
SimRechargeHistory,
sim=self.sim,
product_type=AIRTIME,
_quantity=3
)

assert not self.rule.when(self.sim)

我不断收到此消息:

_________ CommonRuleWhenTestsMixin.test_returns_false_if_rule_inactive _________                                                                                                                                  
simcontrol/rules/tests/test_models.py:14: in test_returns_false_if_rule_inactive
self.rule.active = False
E AttributeError: 'CommonRuleWhenTestsMixin' object has no attribute 'rule'

我的 mixin 如何访问子类在 self 上设置的变量?

最佳答案

你的 mixin 会从 unittest.TestCase 中命中,因此它的测试会被 pytest 拾取(并且可能也会被 unittest 拾取)。

相反,不要从任何东西(或 Python 2 上的 object)继承您的 mixin,并让您的 TestWhen 类继承 unittest.TestCase CommonRuleWhenTestsMixin

关于python - Py.test mixin类无法访问 `self`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39512042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com