[英]Py.test mixin class can't access `self`
我正在嘗試為一組共享的測試制作一個mixin。 我希望能夠在我希望運行那些通用測試時繼承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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.