nose × generator
param_list= [('a', 'a'), ('a', 'b'), ('b', 'b')]
def test_generator():
for params in param_list:
yield check_em, params[0], params[1]
def check_em(a, b):
assert a == b
$ nosetests test_nose_generators.py
.F.
======================================================================
~
File "/Users/../test_nose_generators.py", line 11, in check_em
assert a == b
AssertionError
----------------------------------------------------------------------
Ran 3 tests in 0.002s
FAILED (failures=1)
nosetestsコマンドでtest_xx.pyなファイルを探し
てtest_な関数を実行してくれる。
16.
nose_parameterized
from nose_parameterized importparameterized
import unittest
class TestSequence(unittest.TestCase):
@parameterized.expand(
[
["foo", "a", "b"],
["bar", "b", "b"],
["baz", "a", "a"],
])
def test_sequence(self, name, a, b):
self.assertEqual(a,b)
if __name__ == '__main__':
unittest.main()
FAIL: test_sequence_1_bar (__main__.TestSequence)
----------------------------------------------------------------------
~
File "test_nose_parameterized.py", line 13, in test_sequence
self.assertEqual(a,b)
AssertionError: 'a' != 'b'
- a
+ b
----------------------------------------------------------------------
Ran 3 tests in 0.001s
FAILED (failures=1)
decoratorの引数で
テストの設定を渡す感じ?
17.
動的メソッド定義でWebTestしてみた
import unittest
from webtestimport TestApp
app = TestApp('http://target_api_url')
tests = [["bioproject", "/bioproject/PRJNA25", "In", "Package"],
["organism_name", "/bioproject?organism_name=Papilio%20xuthus", "Greater", 1],
["root", "/sra", "Equal", "200 OK”]]
class TestSequense(unittest.TestCase):
pass
def test_gen(path, method, b):
def Equal(self):
res = app.get(path)
a = res.status
self.assertEqual(a, b)
def Greater(self):
res = app.get(path)
a = res.json['numFound']
self.assertGreater(a, b)
def In(self):
res = app.get(path)
a = res.json.keys()
self.assertIn(b, a)
#return eval(method)
return locals()[method]
if __name__ == '__main__':
for name, path, method, val in tests:
test_name = 'test_{}'.format(name)
test = test_gen(path, method, val)
setattr(TestSequense, test_name, test)
-------------------------------------------------------
Ran 3 tests in 0.298s
OK
でした。
参考サイト
How to generatedynamic (parametrized) unit tests in python?
http://stackoverflow.com/questions/32899/how-to-generate-dynamic-parametrized-unit-tests-in-python
メタプログラミングPython
https://tell-k.github.io/pyconjp2016/#1
Pythonによる黒魔術入門
http://www.slideshare.net/ssuser38b704/ll-lang-blackmagic
Pythonでメタプログラミング
http://qiita.com/fujimisakari/items/0d4786dd9ddeed4eb702