2016-09-15 44 views
0

我想爲共享的一組測試製作一個mixin。我希望能夠隨時從mixin中繼承這些通用測試。Py.test mixin類不能訪問`self`

下面是我的一些混入的:

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上設置的變量?

+0

TestCase這裏是unittest中的一個嗎?你確定'setUp'正在運行嗎? –

+0

是的,是的,因爲我在設置中聲稱False檢查 – Lee

回答

3

你的mixin inershits從unittest.TestCase,所以它的測試被pytest拾取(並且可能會被unittest拾取)。

相反,不從任何東西(或object關於Python 2)繼承你的mixin,並從兩個unittest.TestCaseCommonRuleWhenTestsMixin讓你TestWhen類繼承。