Commits

Tetsuya Morimoto  committed 9e7b682

added the data driven test for nose and pytest

  • Participants
  • Parent commits 8fba4a5

Comments (0)

Files changed (2)

File data_driven_test/test_nose-data-driven.py

+# -*- coding: utf-8 -*-
+
+from nose.tools import ok_, with_setup
+
+PRIME = {2: True, 3: True, 4: False, 5: True, 6: False, 7: True}
+
+def is_prime(num):
+    return PRIME[num]
+
+def test_is_prime():
+    for num in [3, 4, 5]:
+        #print("number: %s" % num)
+        #if num == 4:
+        #    import pdb; pdb.set_trace()
+        yield ok_, is_prime(num)
+
+
+class TestPrimeNumber(object):
+    def setup(self):
+        print("\ncalled setup")
+
+    def teardown(self):
+        print("called teardown")
+
+    def test_is_prime(self):
+        for num in [3, 4, 5]:
+            print("called test_is_prime: {0}".format(num))
+            yield ok_, is_prime(num)
+
+    def test_function(self):
+        print("called test function")
+        ok_(True)
+
+
+def test_factory(): 
+    class FactoryTestCase(object): 
+        def __init__(self, num):
+            self.num = num
+
+        def __call__(self): 
+            print("called is_prime: {0}".format(self.num))
+            ok_(is_prime(self.num))
+
+        def setup(self): 
+            print("\ncalled setup")
+
+        def teardown(self): 
+            print("called teardown")
+
+    for num in [3, 4, 5]:
+        yield FactoryTestCase(num)
+
+
+
+def mysetup():
+    print("\ncalled mysetup")
+
+def myteardown():
+    print("called myteardown")
+
+@with_setup(mysetup, myteardown)
+def test_is_prime_with_setup_teardown():
+    for num in [3, 4, 5]:
+        print("called test_is_prime: {0}".format(num))
+        yield ok_, is_prime(num)

File data_driven_test/test_pytest-data-driven.py

+# -*- coding: utf-8 -*-
+
+import pytest
+
+PRIME = {2: True, 3: True, 4: False, 5: True, 6: False, 7: True}
+
+def is_prime(num):
+    return PRIME[num]
+
+@pytest.mark.parametrize("num", [3, 4, 5])
+def test_is_prime(num):
+    assert is_prime(num)
+
+
+class TestPrimeNumber(object):
+    def setup(self):
+        print("\ncalled setup")
+
+    def teardown(self):
+        print("\ncalled teardown")
+
+    @pytest.mark.parametrize("num", [3, 4, 5])
+    def test_is_prime(self, num):
+        print("called test_is_prime: {0}".format(num))
+        assert is_prime(num)
+
+    def test_function(self):
+        print("called test function")
+        assert True
+
+
+def setup_function(function):
+    print("\ncalled setup: {0}".format(function))
+
+def teardown_function(function):
+    print("\ncalled teardown: {0}".format(function))
+
+@pytest.mark.parametrize("num", [3, 4, 5])
+def test_is_prime2(num):
+    print("called test_is_prime: {0}".format(num))
+    assert is_prime(num)
+
+
+def mysetup():
+    print("\ncalled mysetup")
+    return [3, 4, 5]
+
+def myteardown(nums):
+    print("\ncalled myteardown: {0}".format(nums))
+
+def pytest_funcarg__nums(request):
+    return request.cached_setup(setup=mysetup, teardown=myteardown)
+
+def test_is_prime_funcarg_setup_teardown(nums):
+    print("called test_is_prime: {0}".format(nums))
+    for num in nums:
+        assert is_prime(num)