" />
本ページはプロモーションが含まれています。

スポンサーリンク

開発

テスト駆動開発のxunit.pyを実行したらエラーになる

ケント・ベックの「テスト駆動開発」を写経していて、
第18章 xUnitへ向かう小さな一歩 でつまづきました。

class TestCase:
    def __init__(self, name):
        self.name = name
    def run(self):
        method = getattr(self, self.name)
        method()

class WasRun(TestCase):
    def __init__(self, name):
        self.wasRun = None
        super().__init__(name)
    def testMethod(self):
        self.wasRun = 1

class TestCaseTest(TestCase):
    def testRunning(self):
        test = WasRun("testMethod")
        assert(not test.wasRun)
        test.run()
        assert(test.wasRun)

TestCaseTest("testRunning").run()

問題は実行方法です。
python3で実行しないと、エラーになります。

python3.Xで実行
noricgeographic@MacBook-Air python-xunit % python3 xunit.py

python2.Xで実行
noricgeographic@MacBook-Air python-xunit % python xunit.py 
Traceback (most recent call last):
  File "xunit.py", line 22, in <module>
    TestCaseTest("testRunning").run()
  File "xunit.py", line 6, in run
    method()
  File "xunit.py", line 17, in testRunning
    test = WasRun("testMethod")
  File "xunit.py", line 11, in __init__
    super().__init__(name)
TypeError: super() takes at least 1 argument (0 given)

M1 MacではデフォルトでPython2系が入っているので、Python3系は別途インストールしてください。

https://www.python.org/downloads/release/python-3101/
上記リンクの下の方、「Files」の中から「macOS 64-bit universal2 installer」を選択して、インストールすれば良いです。

↓↓こちらの本です↓↓

スポンサーリンク

-開発