Skip to content

Instantly share code, notes, and snippets.

@TakesxiSximada
Last active October 14, 2016 04:28
Show Gist options
  • Save TakesxiSximada/ae1cfe4794ed3978c005534e9536164d to your computer and use it in GitHub Desktop.
Save TakesxiSximada/ae1cfe4794ed3978c005534e9536164d to your computer and use it in GitHub Desktop.
TestクラスをTestCase以外のクラスもmixinで継承した場合にtest対象に含まれるか確認する

TestクラスをTestCase以外のクラスもmixinで継承した場合にtest対象に含まれるか確認する

Pythonのテストの探索でmixinしても大丈夫かどうかを確認してみます。

test_it.py::

from unittest import TestCase


class Mixin(object):
    def dummy(self):
        pass


class SimpleTest(TestCase, Mixin):
    def test_fail(self):
        self.fail()

unittest

python -m unittest test_it
F
======================================================================
FAIL: test_fail (test_it.SimpleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_it.py", line 11, in test_fail
    self.fail()
AssertionError: None

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)
make: *** [unittest] Error 1

-> される

pytest

pytest
============================================================================================================================= test session starts ==============================================================================================================================
platform darwin -- Python 2.7.12, pytest-3.0.3, py-1.4.31, pluggy-0.4.0
rootdir: /path/to/test, inifile:
collected 1 items

test_it.py F

=================================================================================================================================== FAILURES ===================================================================================================================================
_____________________________________________________________________________________________________________________________ SimpleTest.test_fail _____________________________________________________________________________________________________________________________

self = <test_it.SimpleTest testMethod=test_fail>

    def test_fail(self):
>       self.fail()
E       AssertionError: None

test_it.py:11: AssertionError
=========================================================================================================================== 1 failed in 0.11 seconds ===========================================================================================================================
make: *** [pytest] Error 1


-> される

nose

nosetests
F
======================================================================
FAIL: test_fail (test_it.SimpleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/path/to/test/test_it.py", line 11, in test_fail
    self.fail()
AssertionError: None

----------------------------------------------------------------------
Ran 1 test in 0.061s

FAILED (failures=1)
make: *** [nosetests] Error 1

-> される

結論

  • mixinされてても探索される
  • そもそも関数名で探索している模様
.PHONY: pytest
pytest:
pytest
.PHONY: nosetests
nosetests:
nosetests
.PHONY: unitteset
unittest:
python -m unittest test_it
from unittest import TestCase
class Mixin(object):
def dummy(self):
pass
class SimpleTest(TestCase, Mixin):
def test_fail(self):
self.fail()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment