Update README
diff --git a/README.rst b/README.rst
index c033fcd..a77d3c4 100644
--- a/README.rst
+++ b/README.rst
@@ -11,58 +11,57 @@
.. code:: python
- # test_math.py
- from nose.tools import assert_equal
- from parameterized import parameterized
+ # test_math.py
+ from nose.tools import assert_equal
+ from parameterized import parameterized, parameterized_class
- import unittest
- import math
+ import unittest
+ import math
- @parameterized([
- (2, 2, 4),
- (2, 3, 8),
- (1, 9, 1),
- (0, 9, 0),
- ])
- def test_pow(base, exponent, expected):
- assert_equal(math.pow(base, exponent), expected)
+ @parameterized([
+ (2, 2, 4),
+ (2, 3, 8),
+ (1, 9, 1),
+ (0, 9, 0),
+ ])
+ def test_pow(base, exponent, expected):
+ assert_equal(math.pow(base, exponent), expected)
- class TestMathUnitTest(unittest.TestCase):
- @parameterized.expand([
- ("negative", -1.5, -2.0),
- ("integer", 1, 1.0),
- ("large fraction", 1.6, 1),
- ])
- def test_floor(self, name, input, expected):
- assert_equal(math.floor(input), expected)
+ class TestMathUnitTest(unittest.TestCase):
+ @parameterized.expand([
+ ("negative", -1.5, -2.0),
+ ("integer", 1, 1.0),
+ ("large fraction", 1.6, 1),
+ ])
+ def test_floor(self, name, input, expected):
+ assert_equal(math.floor(input), expected)
- @parameterized.parameterized_class(('a', 'b', 'c'), [
- (0, 5, 6),
- (None, None, None),
- ({}, [], [])
- ])
- class TestMathClass(unittest.TestCase):
- def _assertions(self):
- assert hasattr(self, 'a')
- assert hasattr(self, 'b')
- assert hasattr(self, 'c')
+ @parameterized_class(('a', 'b', 'expected_sum', 'expected_product'), [
+ (1, 2, 3, 2),
+ (5, 5, 10, 25),
+ ])
+ class TestMathClass(unittest.TestCase):
+ def test_add(self):
+ assert_equal(self.a + self.b, self.expected_sum)
- def test_method_a(self):
- self._assertions()
+ def test_multiply(self):
+ assert_equal(self.a * self.b, self.expected_product)
- def test_method_b(self):
- self._assertions()
+ @parameterized_class([
+ { "a": 3, "expected": 2 },
+ { "b": 5, "expected": -4 },
+ ])
+ class TestMathClassDict(unittest.TestCase):
+ a = 1
+ b = 1
+
+ def test_subtract(self):
+ assert_equal(self.a - self.b, self.expected)
With nose (and nose2)::
$ nosetests -v test_math.py
- test_method_a (test_math.TestMathClass_1) ... ok
- test_method_b (test_math.TestMathClass_1) ... ok
- test_method_a (test_math.TestMathClass_2) ... ok
- test_method_b (test_math.TestMathClass_2) ... ok
- test_method_a (test_math.TestMathClass_3) ... ok
- test_method_b (test_math.TestMathClass_3) ... ok
test_floor_0_negative (test_math.TestMathUnitTest) ... ok
test_floor_1_integer (test_math.TestMathUnitTest) ... ok
test_floor_2_large_fraction (test_math.TestMathUnitTest) ... ok
@@ -70,9 +69,14 @@
test_math.test_pow(2, 3, 8, {}) ... ok
test_math.test_pow(1, 9, 1, {}) ... ok
test_math.test_pow(0, 9, 0, {}) ... ok
+ test_add (test_math.TestMathClass_0) ... ok
+ test_multiply (test_math.TestMathClass_0) ... ok
+ test_add (test_math.TestMathClass_1) ... ok
+ test_multiply (test_math.TestMathClass_1) ... ok
+ test_subtract (test_math.TestMathClassDict_0) ... ok
----------------------------------------------------------------------
- Ran 13 tests in 0.015s
+ Ran 12 tests in 0.015s
OK
@@ -87,12 +91,6 @@
platform darwin -- Python 3.6.1, pytest-3.1.3, py-1.4.34, pluggy-0.4.0
collecting ... collected 13 items
- test_math.py::TestMathClass_1::test_method_a PASSED
- test_math.py::TestMathClass_1::test_method_b PASSED
- test_math.py::TestMathClass_2::test_method_a PASSED
- test_math.py::TestMathClass_2::test_method_b PASSED
- test_math.py::TestMathClass_3::test_method_a PASSED
- test_math.py::TestMathClass_3::test_method_b PASSED
test_math.py::test_pow::[0] PASSED
test_math.py::test_pow::[1] PASSED
test_math.py::test_pow::[2] PASSED
@@ -100,23 +98,27 @@
test_math.py::TestMathUnitTest::test_floor_0_negative PASSED
test_math.py::TestMathUnitTest::test_floor_1_integer PASSED
test_math.py::TestMathUnitTest::test_floor_2_large_fraction PASSED
- ==================== 13 passed, 4 warnings in 0.16 seconds =====================
+ test_math.py::TestMathClass_0::test_add PASSED
+ test_math.py::TestMathClass_0::test_multiply PASSED
+ test_math.py::TestMathClass_1::test_add PASSED
+ test_math.py::TestMathClass_1::test_multiply PASSED
+ test_math.py::TestMathClassDict_0::test_subtract PASSED
+ ==================== 12 passed, 4 warnings in 0.16 seconds =====================
With unittest (and unittest2)::
$ python -m unittest -v test_math
- test_method_a (test_math.TestMathClass_1) ... ok
- test_method_b (test_math.TestMathClass_1) ... ok
- test_method_a (test_math.TestMathClass_2) ... ok
- test_method_b (test_math.TestMathClass_2) ... ok
- test_method_a (test_math.TestMathClass_3) ... ok
- test_method_b (test_math.TestMathClass_3) ... ok
test_floor_0_negative (test_math.TestMathUnitTest) ... ok
test_floor_1_integer (test_math.TestMathUnitTest) ... ok
test_floor_2_large_fraction (test_math.TestMathUnitTest) ... ok
+ test_add (test_math.TestMathClass_0) ... ok
+ test_multiply (test_math.TestMathClass_0) ... ok
+ test_add (test_math.TestMathClass_1) ... ok
+ test_multiply (test_math.TestMathClass_1) ... ok
+ test_subtract (test_math.TestMathClassDict_0) ... ok
----------------------------------------------------------------------
- Ran 9 tests in 0.001s
+ Ran 8 tests in 0.001s
OK
@@ -140,8 +142,16 @@
. test_floor_0_negative
. test_floor_1_integer
. test_floor_2_large_fraction
+ TestMathClass_0
+ . test_add
+ . test_multiply
+ TestMathClass_1
+ . test_add
+ . test_multiply
+ TestMathClassDict_0
+ . test_subtract
- Ran 9 tests in 0.121s
+ Ran 12 tests in 0.121s
OK (passes=9)
@@ -168,8 +178,10 @@
* -
- Py2.6
- Py2.7
- - Py3.3
- Py3.4
+ - Py3.5
+ - Py3.6
+ - Py3.7
- PyPy
* - nose
- yes
@@ -177,18 +189,24 @@
- yes
- yes
- yes
+ - yes
+ - yes
* - nose2
- yes
- yes
- yes
- yes
- yes
+ - yes
+ - yes
* - py.test
- yes
- yes
- yes
- yes
- yes
+ - yes
+ - yes
* - | unittest
| (``@parameterized.expand``)
- yes
@@ -196,6 +214,8 @@
- yes
- yes
- yes
+ - yes
+ - yes
* - | unittest2
| (``@parameterized.expand``)
- yes
@@ -203,6 +223,8 @@
- yes
- yes
- yes
+ - yes
+ - yes
Dependencies
------------
@@ -409,33 +431,47 @@
OK
-Finally ``@parameterized.expand_class`` parameterizes an entire class:
+Finally ``@parameterized.expand_class`` parameterizes an entire class, using
+either a list of attributes, or a list of dicts that will be applied to the
+class:
.. code:: python
- from app.models import User
- from django.test import TestCase
- from parameterized import parameterized
+ from yourapp.models import User
+ from parameterized import parameterized_class
- @parameterized.parameterized_class(('username', 'access_level'), [
- ('user_1', 1),
- ('user_2', 2)
- ])
- class TestUserAccessLevel(TestCase):
- def setUp(self):
- self.client.force_login(User.objects.get(username=self.username)[0])
+ @parameterized_class(("username", "access_level", "expected_status_code"), [
+ ("user_1", 1, 200),
+ ("user_2", 2, 404)
+ ])
+ class TestUserAccessLevel(TestCase):
+ def setUp(self):
+ self.client.force_login(User.objects.get(username=self.username)[0])
- def test_url_a(self):
- response = self.client.get('/url')
- if(self.access_level == 1):
- self.assertEqual(response.status_code, 200)
- else:
- self.assertNotEqual(response.status_code, 200)
+ def test_url_a(self):
+ response = self.client.get("/url")
+ self.assertEqual(response.status_code, self.expected_status_code)
- def tearDown(self):
- self.client.logout()
+ def tearDown(self):
+ self.client.logout()
+ @parameterized_class([
+ { "username": "user_1", "access_level": 1 },
+ { "username": "user_2", "access_level": 2, "expected_status_code": 404 },
+ ])
+ class TestUserAccessLevel(TestCase):
+ expected_status_code = 200
+
+ def setUp(self):
+ self.client.force_login(User.objects.get(username=self.username)[0])
+
+ def test_url_a(self):
+ response = self.client.get('/url')
+ self.assertEqual(response.status_code, self.expected_status_code)
+
+ def tearDown(self):
+ self.client.logout()
Migrating from ``nose-parameterized`` to ``parameterized``