Skip to content

Commit

Permalink
Merge pull request #165 from bayandin/cartesian-params
Browse files Browse the repository at this point in the history
Add decorator cartesian_params (resolve #162)
  • Loading branch information
thedrow committed Apr 26, 2014
2 parents 1ebe8ca + b5a5e56 commit c1f5544
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 6 deletions.
102 changes: 99 additions & 3 deletions nose2/tests/unit/test_params_plugin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from nose2 import events, loader, session, util
from nose2.plugins.loader import parameters, testcases
from nose2.tests._common import TestCase
from nose2.tools import params
from nose2.tools import cartesian_params, params


class TestParams(TestCase):
Expand All @@ -26,7 +26,7 @@ def test():
self.session.hooks.loadTestsFromModule(event)
self.assertEqual(len(event.extraTests), 0)

def test_can_load_tests_from_parameterized_functions(self):
def test_can_load_tests_from_parameterized_by_params_functions(self):
class Mod(object):
__name__ = 'themod'

Expand All @@ -48,7 +48,36 @@ def test(a):
self.assertEqual(util.test_name(event.extraTests[1]),
'themod.test:2')

def test_can_load_tests_from_parameterized_methods(self):
def test_can_load_tests_from_parameterized_by_cartesian_params_functions(self):
class Mod(object):
__name__ = 'themod'

def check(x, y):
assert x == y

@cartesian_params(
(1, 2),
(2, 3),
)
def test(a, b):
check(a, b)
m = Mod()
m.test = test
test.__module__ = m.__name__
event = events.LoadFromModuleEvent(self.loader, m)
self.session.hooks.loadTestsFromModule(event)
self.assertEqual(len(event.extraTests), 4)
# check that test names are sensible
self.assertEqual(util.test_name(event.extraTests[0]),
'themod.test:1')
self.assertEqual(util.test_name(event.extraTests[1]),
'themod.test:2')
self.assertEqual(util.test_name(event.extraTests[2]),
'themod.test:3')
self.assertEqual(util.test_name(event.extraTests[3]),
'themod.test:4')

def test_can_load_tests_from_parameterized_by_params_methods(self):
class Mod(object):
__name__ = 'themod'

Expand All @@ -69,3 +98,70 @@ def test(self, a):
'themod.Test.test:1')
self.assertEqual(util.test_name(event.extraTests[0]._tests[1]),
'themod.Test.test:2')

def test_can_load_tests_from_parameterized_by_cartesian_params_methods(self):
class Mod(object):
__name__ = 'themod'

class Test(TestCase):

@cartesian_params(
(1, 2),
(2, 3),
)
def test(self, a, b):
assert a == b
m = Mod()
m.Test = Test
Test.__module__ = m.__name__
event = events.LoadFromModuleEvent(self.loader, m)
self.session.hooks.loadTestsFromModule(event)
self.assertEqual(len(event.extraTests), 1)
self.assertEqual(len(event.extraTests[0]._tests), 4)
# check that test names are sensible
self.assertEqual(util.test_name(event.extraTests[0]._tests[0]),
'themod.Test.test:1')
self.assertEqual(util.test_name(event.extraTests[0]._tests[1]),
'themod.Test.test:2')
self.assertEqual(util.test_name(event.extraTests[0]._tests[2]),
'themod.Test.test:3')
self.assertEqual(util.test_name(event.extraTests[0]._tests[3]),
'themod.Test.test:4')

def test_params_creates_params_for_function(self):
@params(
(1, 2),
('a', 'b'),
)
def test(a, b):
assert a == b
self.assertTupleEqual(tuple(test.paramList), ((1, 2), ('a', 'b')))

def test_cartesian_params_creates_cartesian_product_of_params_for_function(self):
@cartesian_params(
(1, 2),
('a', 'b'),
)
def test(a, b):
assert a == b
self.assertTupleEqual(tuple(test.paramList), ((1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')))

def test_params_creates_params_for_method(self):
class Test(TestCase):
@params(
(1, 2),
('a', 'b'),
)
def test(self, a, b):
assert a == b
self.assertTupleEqual(tuple(Test.test.paramList), ((1, 2), ('a', 'b')))

def test_cartesian_params_creates_cartesian_product_of_params_for_method(self):
class Test(TestCase):
@cartesian_params(
(1, 2),
('a', 'b'),
)
def test(self, a, b):
assert a == b
self.assertTupleEqual(tuple(Test.test.paramList), ((1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')))
4 changes: 2 additions & 2 deletions nose2/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .params import params
from .params import cartesian_params, params
from . import such

__all__ = ['params', 'such']
__all__ = ['cartesian_params', 'params', 'such']
36 changes: 35 additions & 1 deletion nose2/tools/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,45 @@
Rights Reserved. See: http://docs.python.org/license.html
"""
import itertools

__unittest = True


def cartesian_params(*paramList):
"""Make a test function or method parameterized by cartesian product
of parameters
.. code-block :: python
import unittest
from nose2.tools import cartesian_params
@cartesian_params((1, 2, 3), ('a', 'b'))
def test_nums(num, char):
assert num < ord(char)
class Test(unittest.TestCase):
@cartesian_params((1, 2, 3), ('a', 'b'))
def test_less_than(self, num, char):
self.assertLess(num, ord(char))
Parameters in the list must be defined as iterable objects such as
tuple or list.
"""
def decorator(func):
func.paramList = itertools.product(*paramList)
return func
return decorator


def params(*paramList):
"""Make a test function or method parameterized.
"""Make a test function or method parameterized by parameters.
.. code-block :: python
Expand Down

0 comments on commit c1f5544

Please sign in to comment.