Py.test mixin class can't access `self`
<p>I am trying to make a mixin for a shared set of tests. I want to be able to inherit from the mixin whenever I want those generic tests to run.</p>

<p>Here is some of my mixin:</p>

<pre><code>class CommonRuleWhenTestsMixin(TestCase):

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

        assert not self.rule.when(self.sim)
</code></pre>

<p>Here is when I use the mixin:</p>

<pre><code>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)
</code></pre>

<p>I keep getting this message:</p>

<pre><code>_________ 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'   
</code></pre>

<p>How can my mixin access the variables set on <code>self</code> by the child class?</p>

<p>Your mixin inerhits from <code>unittest.TestCase</code>, so its test gets picked up by pytest (and would probably get picked up by <code>unittest</code> as well).</p>

<p>Instead, don't inherit your mixin from anything (or from <code>object</code> on Python 2), and make your <code>TestWhen</code> class inherit from both <code>unittest.TestCase</code> and <code>CommonRuleWhenTestsMixin</code>.</p>

python py.test