テスト駆動開発入門をPythonで写経してみた。1をpikzieでテストを書き直してみました。
money.py
テスト駆動開発入門をPythonで写経してみた。1から少し書き換わっています。
#!/usr/bin/env python # coding: utf-8 """ テスト駆動開発入門1章 """ class Dollar(object): def __init__(self, amount): self.amount = amount def times(self, multiplier): self.amount *= multiplier
money_pikzie_test.py
"test_"から始まるメソッドを実行します。アサーションがPyUnitみたいにキャメルケースじゃなくていい感じ。if __name__ == "__main__":を書かないのもいい感じ。
#!/usr/bin/env python # coding: utf-8 """ テスト駆動開発入門1章 """ import pikzie from money import Dollar class TestMoney(pikzie.TestCase): def test_multiplication(self): five = Dollar(5) five.times(2) self.assert_equal(10, five.amount)
money_nose_test.py
ついでにnoseでも。vim上での実行コマンドはこんな感じ。
:!nosetests money_nose_test.py
pythonじゃなくnosetestsってとこがミソですかね。ちょっと違和感があります。
#!/usr/bin/env python # coding: utf-8 """ テスト駆動開発入門1章 複数通貨のMoneyオブジェクト """ from nose.tools import * from money import Dollar class TestMoney(object): def test_multiplication(self): """かけ算のテスト """ five = Dollar(5) five.times(2) assert_equal(10, five.amount)
下記のコードを付け足すと通常のpythonスクリプトと同様に実行できるらしい。
if __name__ == '__main__': import nose nose.main()
うちの環境(WinXP + Python2.5)だと思いっきりエラーがでます。何か間違えているのかな?
C:\WINDOWS\system32\cmd.exe /c python money_nose_test.py
.Traceback (most recent call last):
File "money_nose_test.py", line 19, in <module>
nose.main()
File "d:\python25\lib\site-packages\nose-0.11.1-py2.5.egg\nose\core.py", line 113, in __init__
argv=argv, testRunner=testRunner, testLoader=testLoader)
File "D:\Python25\lib\unittest.py", line 768, in __init__
self.runTests()
File "d:\python25\lib\site-packages\nose-0.11.1-py2.5.egg\nose\core.py", line 192, in runTests
result = self.testRunner.run(self.test)
File "d:\python25\lib\site-packages\nose-0.11.1-py2.5.egg\nose\core.py", line 61, in run
test(result)
File "d:\python25\lib\site-packages\nose-0.11.1-py2.5.egg\nose\suite.py", line 154, in __call__
return self.run(*arg, **kw)
File "d:\python25\lib\site-packages\nose-0.11.1-py2.5.egg\nose\suite.py", line 197, in run
test(orig)
File "D:\Python25\lib\unittest.py", line 437, in __call__
return self.run(*args, **kwds)
File "D:\Python25\lib\unittest.py", line 430, in run
for test in self._tests:
File "d:\python25\lib\site-packages\nose-0.11.1-py2.5.egg\nose\suite.py", line 83, in _get_tests
for test in self.test_generator:
File "d:\python25\lib\site-packages\nose-0.11.1-py2.5.egg\nose\loader.py", line 157, in loadTestsFromDir
entry_path, discovered=True)
File "d:\python25\lib\site-packages\nose-0.11.1-py2.5.egg\nose\loader.py", line 394, in loadTestsFromName
discovered=discovered)
File "d:\python25\lib\site-packages\nose-0.11.1-py2.5.egg\nose\loader.py", line 305, in loadTestsFromModule
test_classes + test_funcs)
File "d:\python25\lib\site-packages\nose-0.11.1-py2.5.egg\nose\loader.py", line 304, in <lambda>
tests = map(lambda t: self.makeTest(t, parent=module),
File "d:\python25\lib\site-packages\nose-0.11.1-py2.5.egg\nose\loader.py", line 511, in makeTest
return self.loadTestsFromTestClass(obj)
File "d:\python25\lib\site-packages\nose-0.11.1-py2.5.egg\nose\loader.py", line 475, in loadTestsFromTestClass
for case in filter(wanted, dir(cls))]
File "d:\python25\lib\site-packages\nose-0.11.1-py2.5.egg\nose\loader.py", line 521, in makeTest
return MethodTestCase(obj)
File "d:\python25\lib\site-packages\nose-0.11.1-py2.5.egg\nose\case.py", line 328, in __init__
self.inst = self.cls()
TypeError: type() takes 1 or 3 arguments
Hit any key to close this window...