From 2907ab614964e5838047566c7477b16172717b92 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Tue, 22 Apr 2025 10:38:49 +0000 Subject: [PATCH] Format with ruff --- .github/workflows/python-package.yml | 3 + doc/__init__.py | 4 +- doc/example.py | 11 +- doc/test_sample.py | 6 +- setup.py | 4 +- testscenarios/__init__.py | 28 ++-- testscenarios/scenarios.py | 45 +++--- testscenarios/testcase.py | 27 ++-- testscenarios/tests/__init__.py | 15 +- testscenarios/tests/test_scenarios.py | 188 ++++++++++++++------------ testscenarios/tests/test_testcase.py | 63 +++++---- 11 files changed, 210 insertions(+), 184 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 552dbde..bbeb64b 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -26,6 +26,9 @@ jobs: - name: Run ruff run: | python -m ruff check . + - name: Run ruff format + run: | + python -m ruff format --check . - name: Test with testtools run: | python -m testtools.run testscenarios.tests.test_suite diff --git a/doc/__init__.py b/doc/__init__.py index 4dbad55..2c14d37 100644 --- a/doc/__init__.py +++ b/doc/__init__.py @@ -2,12 +2,12 @@ # dependency injection ('scenarios') by tests. # # Copyright (c) 2009, Robert Collins -# +# # Licensed under either the Apache License, Version 2.0 or the BSD 3-clause # license at the users choice. A copy of both licenses are available in the # project source as Apache-2.0 and BSD. You may not use this file except in # compliance with one of these two licences. -# +# # Unless required by applicable law or agreed to in writing, software # distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the diff --git a/doc/example.py b/doc/example.py index a8d195f..666c0c3 100644 --- a/doc/example.py +++ b/doc/example.py @@ -1,12 +1,12 @@ # testscenarios: extensions to python unittest to allow declarative # dependency injection ('scenarios') by tests. # Copyright (c) 2009, Robert Collins -# +# # Licensed under either the Apache License, Version 2.0 or the BSD 3-clause # license at the users choice. A copy of both licenses are available in the # project source as Apache-2.0 and BSD. You may not use this file except in # compliance with one of these two licences. -# +# # Unless required by applicable law or agreed to in writing, software # distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the @@ -18,13 +18,12 @@ from testscenarios import TestWithScenarios -scenario1 = ('basic', {'attribute': 'value'}) -scenario2 = ('advanced', {'attribute': 'value2'}) +scenario1 = ("basic", {"attribute": "value"}) +scenario2 = ("advanced", {"attribute": "value2"}) class SampleWithScenarios(TestWithScenarios): - scenarios = [scenario1, scenario2] - + def test_demo(self): self.assertIsInstance(self.attribute, str) diff --git a/doc/test_sample.py b/doc/test_sample.py index a0b00a5..fa3be1c 100644 --- a/doc/test_sample.py +++ b/doc/test_sample.py @@ -2,12 +2,12 @@ # dependency injection ('scenarios') by tests. # # Copyright (c) 2009, Robert Collins -# +# # Licensed under either the Apache License, Version 2.0 or the BSD 3-clause # license at the users choice. A copy of both licenses are available in the # project source as Apache-2.0 and BSD. You may not use this file except in # compliance with one of these two licences. -# +# # Unless required by applicable law or agreed to in writing, software # distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the @@ -16,7 +16,7 @@ import unittest -class TestSample(unittest.TestCase): +class TestSample(unittest.TestCase): def test_so_easy(self): pass diff --git a/setup.py b/setup.py index ed58d0f..2d8359f 100755 --- a/setup.py +++ b/setup.py @@ -1,6 +1,4 @@ #!/usr/bin/env python import setuptools -setuptools.setup( - setup_requires=['pbr'], - pbr=True) +setuptools.setup(setup_requires=["pbr"], pbr=True) diff --git a/testscenarios/__init__.py b/testscenarios/__init__.py index ab68ba9..fa2d529 100644 --- a/testscenarios/__init__.py +++ b/testscenarios/__init__.py @@ -2,12 +2,12 @@ # dependency injection ('scenarios') by tests. # # Copyright (c) 2009, Robert Collins -# +# # Licensed under either the Apache License, Version 2.0 or the BSD 3-clause # license at the users choice. A copy of both licenses are available in the # project source as Apache-2.0 and BSD. You may not use this file except in # compliance with one of these two licences. -# +# # Unless required by applicable law or agreed to in writing, software # distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the @@ -39,20 +39,21 @@ # If the releaselevel is 'final', then the tarball will be major.minor.micro. # Otherwise it is major.minor.micro~$(revno). from pbr.version import VersionInfo -_version = VersionInfo('testscenarios') + +_version = VersionInfo("testscenarios") __version__ = _version.semantic_version().version_tuple() version = _version.release_string() __all__ = [ - 'TestWithScenarios', - 'WithScenarios', - 'apply_scenario', - 'apply_scenarios', - 'generate_scenarios', - 'load_tests_apply_scenarios', - 'multiply_scenarios', - 'per_module_scenarios', - ] + "TestWithScenarios", + "WithScenarios", + "apply_scenario", + "apply_scenarios", + "generate_scenarios", + "load_tests_apply_scenarios", + "multiply_scenarios", + "per_module_scenarios", +] from testscenarios.scenarios import ( # noqa: E402 @@ -62,12 +63,13 @@ load_tests_apply_scenarios, multiply_scenarios, per_module_scenarios, - ) +) from testscenarios.testcase import TestWithScenarios, WithScenarios # noqa: E402 def test_suite(): import testscenarios.tests # noqa: F401 + return testscenarios.tests.test_suite() diff --git a/testscenarios/scenarios.py b/testscenarios/scenarios.py index edb570e..30a902e 100644 --- a/testscenarios/scenarios.py +++ b/testscenarios/scenarios.py @@ -3,12 +3,12 @@ # # Copyright (c) 2009, Robert Collins # Copyright (c) 2010, 2011 Martin Pool -# +# # Licensed under either the Apache License, Version 2.0 or the BSD 3-clause # license at the users choice. A copy of both licenses are available in the # project source as Apache-2.0 and BSD. You may not use this file except in # compliance with one of these two licences. -# +# # Unless required by applicable law or agreed to in writing, software # distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the @@ -16,16 +16,16 @@ # limitations under that license. __all__ = [ - 'apply_scenario', - 'apply_scenarios', - 'generate_scenarios', - 'load_tests_apply_scenarios', - 'multiply_scenarios', - ] + "apply_scenario", + "apply_scenarios", + "generate_scenarios", + "load_tests_apply_scenarios", + "multiply_scenarios", +] from itertools import ( product, - ) +) import sys from testtools.testcase import clone_test_with_new_id @@ -42,13 +42,12 @@ def apply_scenario(scenario, test): :return: A new test cloned from test, with the scenario applied. """ name, parameters = scenario - scenario_suffix = '(' + name + ')' - newtest = clone_test_with_new_id(test, - test.id() + scenario_suffix) + scenario_suffix = "(" + name + ")" + newtest = clone_test_with_new_id(test, test.id() + scenario_suffix) test_desc = test.shortDescription() if test_desc is not None: newtest_desc = "%(test_desc)s %(scenario_suffix)s" % vars() - newtest.shortDescription = (lambda: newtest_desc) + newtest.shortDescription = lambda: newtest_desc for key, value in parameters.items(): setattr(newtest, key, value) return newtest @@ -76,7 +75,7 @@ def generate_scenarios(test_or_suite): :return: A generator of tests - objects satisfying the TestCase protocol. """ for test in iterate_tests(test_or_suite): - scenarios = getattr(test, 'scenarios', None) + scenarios = getattr(test, "scenarios", None) if scenarios: for newtest in apply_scenarios(scenarios, test): newtest.scenarios = None @@ -98,10 +97,10 @@ def load_tests_apply_scenarios(*params): pattern), and bzr used (standard_tests, module, loader). :param loader: A TestLoader. - :param standard_test: The test objects found in this module before + :param standard_test: The test objects found in this module before multiplication. """ - if getattr(params[0], 'suiteClass', None) is not None: + if getattr(params[0], "suiteClass", None) is not None: loader, standard_tests, pattern = params else: standard_tests, module, loader = params @@ -115,7 +114,7 @@ def multiply_scenarios(*scenarios): It is safe to pass scenario generators or iterators. - :returns: A list of compound scenarios: the cross-product of all + :returns: A list of compound scenarios: the cross-product of all scenarios, with the names concatenated and the parameters merged together. """ @@ -123,7 +122,7 @@ def multiply_scenarios(*scenarios): scenario_lists = map(list, scenarios) for combination in product(*scenario_lists): names, parameters = zip(*combination) - scenario_name = ','.join(names) + scenario_name = ",".join(names) scenario_parameters = {} for parameter in parameters: scenario_parameters.update(parameter) @@ -145,10 +144,10 @@ def per_module_scenarios(attribute_name, modules): yet. :param attribute_name: A name to be set in the scenario parameter - dictionary (and thence onto the test instance) pointing to the + dictionary (and thence onto the test instance) pointing to the implementation module (or import exception) for this scenario. - :param modules: An iterable of (short_name, module_name), where + :param modules: An iterable of (short_name, module_name), where the short name is something like 'python' to put in the scenario name, and the long name is a fully-qualified Python module name. @@ -156,10 +155,8 @@ def per_module_scenarios(attribute_name, modules): scenarios = [] for short_name, module_name in modules: try: - mod = __import__(module_name, {}, {}, ['']) + mod = __import__(module_name, {}, {}, [""]) except BaseException: mod = sys.exc_info() - scenarios.append(( - short_name, - {attribute_name: mod})) + scenarios.append((short_name, {attribute_name: mod})) return scenarios diff --git a/testscenarios/testcase.py b/testscenarios/testcase.py index b9a9f32..85d3cb1 100644 --- a/testscenarios/testcase.py +++ b/testscenarios/testcase.py @@ -2,12 +2,12 @@ # dependency injection ('scenarios') by tests. # # Copyright (c) 2009, Robert Collins -# +# # Licensed under either the Apache License, Version 2.0 or the BSD 3-clause # license at the users choice. A copy of both licenses are available in the # project source as Apache-2.0 and BSD. You may not use this file except in # compliance with one of these two licences. -# +# # Unless required by applicable law or agreed to in writing, software # distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the @@ -15,9 +15,9 @@ # limitations under that license. __all__ = [ - 'TestWithScenarios', - 'WithScenarios', - ] + "TestWithScenarios", + "WithScenarios", +] import unittest @@ -32,12 +32,16 @@ compatibly with WithScenarios). """ + class WithScenarios(object): - __doc__ = """A mixin for TestCase with support for declarative scenarios. - """ + _doc + __doc__ = ( + """A mixin for TestCase with support for declarative scenarios. + """ + + _doc + ) def _get_scenarios(self): - return getattr(self, 'scenarios', None) + return getattr(self, "scenarios", None) def countTestCases(self): scenarios = self._get_scenarios() @@ -65,5 +69,8 @@ def run(self, result=None): class TestWithScenarios(WithScenarios, unittest.TestCase): - __doc__ = """Unittest TestCase with support for declarative scenarios. - """ + _doc + __doc__ = ( + """Unittest TestCase with support for declarative scenarios. + """ + + _doc + ) diff --git a/testscenarios/tests/__init__.py b/testscenarios/tests/__init__.py index c27abe3..a78fc33 100644 --- a/testscenarios/tests/__init__.py +++ b/testscenarios/tests/__init__.py @@ -2,12 +2,12 @@ # dependency injection ('scenarios') by tests. # # Copyright (c) 2009, Robert Collins -# +# # Licensed under either the Apache License, Version 2.0 or the BSD 3-clause # license at the users choice. A copy of both licenses are available in the # project source as Apache-2.0 and BSD. You may not use this file except in # compliance with one of these two licences. -# +# # Unless required by applicable law or agreed to in writing, software # distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the @@ -23,20 +23,21 @@ def test_suite(): standard_tests = unittest.TestSuite() - module = sys.modules['testscenarios.tests'] + module = sys.modules["testscenarios.tests"] loader = unittest.TestLoader() return load_tests(standard_tests, module, loader) def load_tests(standard_tests, module, loader): test_modules = [ - 'testcase', - 'scenarios', - ] + "testcase", + "scenarios", + ] prefix = "testscenarios.tests.test_" test_mod_names = [prefix + test_module for test_module in test_modules] standard_tests.addTests(loader.loadTestsFromNames(test_mod_names)) doctest.set_unittest_reportflags(doctest.REPORT_ONLY_FIRST_FAILURE) standard_tests.addTest( - doctest.DocFileSuite("../../README.rst", optionflags=doctest.ELLIPSIS)) + doctest.DocFileSuite("../../README.rst", optionflags=doctest.ELLIPSIS) + ) return loader.suiteClass(testscenarios.generate_scenarios(standard_tests)) diff --git a/testscenarios/tests/test_scenarios.py b/testscenarios/tests/test_scenarios.py index ec4077d..c6eebb1 100644 --- a/testscenarios/tests/test_scenarios.py +++ b/testscenarios/tests/test_scenarios.py @@ -3,12 +3,12 @@ # # Copyright (c) 2009, Robert Collins # Copyright (c) 2010, 2011 Martin Pool -# +# # Licensed under either the Apache License, Version 2.0 or the BSD 3-clause # license at the users choice. A copy of both licenses are available in the # project source as Apache-2.0 and BSD. You may not use this file except in # compliance with one of these two licences. -# +# # Unless required by applicable law or agreed to in writing, software # distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the @@ -29,18 +29,20 @@ generate_scenarios, load_tests_apply_scenarios, multiply_scenarios, - ) +) class TestGenerateScenarios(testtools.TestCase): - def hook_apply_scenarios(self): - self.addCleanup(setattr, testscenarios.scenarios, 'apply_scenarios', - apply_scenarios) + self.addCleanup( + setattr, testscenarios.scenarios, "apply_scenarios", apply_scenarios + ) log = [] + def capture(scenarios, test): log.append((scenarios, test)) return apply_scenarios(scenarios, test) + testscenarios.scenarios.apply_scenarios = capture return log @@ -48,6 +50,7 @@ def test_generate_scenarios_preserves_normal_test(self): class ReferenceTest(unittest.TestCase): def test_pass(self): pass + test = ReferenceTest("test_pass") log = self.hook_apply_scenarios() self.assertEqual([test], list(generate_scenarios(test))) @@ -55,35 +58,36 @@ def test_pass(self): def test_tests_with_scenarios_calls_apply_scenarios(self): class ReferenceTest(unittest.TestCase): - scenarios = [('demo', {})] + scenarios = [("demo", {})] + def test_pass(self): pass + test = ReferenceTest("test_pass") log = self.hook_apply_scenarios() tests = list(generate_scenarios(test)) - self.expectThat( - tests[0].id(), EndsWith('ReferenceTest.test_pass(demo)')) - self.assertEqual([([('demo', {})], test)], log) + self.expectThat(tests[0].id(), EndsWith("ReferenceTest.test_pass(demo)")) + self.assertEqual([([("demo", {})], test)], log) def test_all_scenarios_yielded(self): class ReferenceTest(unittest.TestCase): - scenarios = [('1', {}), ('2', {})] + scenarios = [("1", {}), ("2", {})] + def test_pass(self): pass + test = ReferenceTest("test_pass") tests = list(generate_scenarios(test)) - self.expectThat( - tests[0].id(), EndsWith('ReferenceTest.test_pass(1)')) - self.expectThat( - tests[1].id(), EndsWith('ReferenceTest.test_pass(2)')) + self.expectThat(tests[0].id(), EndsWith("ReferenceTest.test_pass(1)")) + self.expectThat(tests[1].id(), EndsWith("ReferenceTest.test_pass(2)")) def test_scenarios_attribute_cleared(self): class ReferenceTest(unittest.TestCase): - scenarios = [ - ('1', {'foo': 1, 'bar': 2}), - ('2', {'foo': 2, 'bar': 4})] + scenarios = [("1", {"foo": 1, "bar": 2}), ("2", {"foo": 2, "bar": 4})] + def test_check_foo(self): pass + test = ReferenceTest("test_check_foo") tests = list(generate_scenarios(test)) for adapted in tests: @@ -91,13 +95,17 @@ def test_check_foo(self): def test_multiple_tests(self): class Reference1(unittest.TestCase): - scenarios = [('1', {}), ('2', {})] + scenarios = [("1", {}), ("2", {})] + def test_something(self): pass + class Reference2(unittest.TestCase): - scenarios = [('3', {}), ('4', {})] + scenarios = [("3", {}), ("4", {})] + def test_something(self): pass + suite = unittest.TestSuite() suite.addTest(Reference1("test_something")) suite.addTest(Reference2("test_something")) @@ -106,32 +114,32 @@ def test_something(self): class TestApplyScenario(testtools.TestCase): - def setUp(self): super(TestApplyScenario, self).setUp() - self.scenario_name = 'demo' - self.scenario_attrs = {'foo': 'bar'} + self.scenario_name = "demo" + self.scenario_attrs = {"foo": "bar"} self.scenario = (self.scenario_name, self.scenario_attrs) class ReferenceTest(unittest.TestCase): def test_pass(self): pass + def test_pass_with_docstring(self): - """ The test that always passes. + """The test that always passes. - This test case has a PEP 257 conformant docstring, - with its first line being a brief synopsis and the - rest of the docstring explaining that this test - does nothing but pass unconditionally. + This test case has a PEP 257 conformant docstring, + with its first line being a brief synopsis and the + rest of the docstring explaining that this test + does nothing but pass unconditionally. - """ + """ pass self.ReferenceTest = ReferenceTest def test_sets_specified_id(self): - raw_test = self.ReferenceTest('test_pass') + raw_test = self.ReferenceTest("test_pass") raw_id = "ReferenceTest.test_pass" scenario_name = self.scenario_name expect_id = f"{raw_id}({scenario_name})" @@ -139,12 +147,12 @@ def test_sets_specified_id(self): self.expectThat(modified_test.id(), EndsWith(expect_id)) def test_sets_specified_attributes(self): - raw_test = self.ReferenceTest('test_pass') + raw_test = self.ReferenceTest("test_pass") modified_test = apply_scenario(self.scenario, raw_test) - self.assertEqual('bar', modified_test.foo) + self.assertEqual("bar", modified_test.foo) def test_appends_scenario_name_to_short_description(self): - raw_test = self.ReferenceTest('test_pass_with_docstring') + raw_test = self.ReferenceTest("test_pass_with_docstring") modified_test = apply_scenario(self.scenario, raw_test) raw_doc = cast(str, self.ReferenceTest.test_pass_with_docstring.__doc__) raw_desc = raw_doc.split("\n")[0].strip() @@ -154,109 +162,109 @@ def test_appends_scenario_name_to_short_description(self): class TestApplyScenarios(testtools.TestCase): - def test_calls_apply_scenario(self): - self.addCleanup(setattr, testscenarios.scenarios, 'apply_scenario', - apply_scenario) + self.addCleanup( + setattr, testscenarios.scenarios, "apply_scenario", apply_scenario + ) log = [] + def capture(scenario, test): log.append((scenario, test)) + testscenarios.scenarios.apply_scenario = capture scenarios = ["foo", "bar"] list(apply_scenarios(scenarios, "test")) - self.assertEqual([('foo', 'test'), ('bar', 'test')], log) + self.assertEqual([("foo", "test"), ("bar", "test")], log) def test_preserves_scenarios_attribute(self): class ReferenceTest(unittest.TestCase): - scenarios = [('demo', {})] + scenarios = [("demo", {})] + def test_pass(self): pass + test = ReferenceTest("test_pass") tests = list(apply_scenarios(ReferenceTest.scenarios, test)) - self.assertEqual([('demo', {})], ReferenceTest.scenarios) + self.assertEqual([("demo", {})], ReferenceTest.scenarios) self.assertEqual(ReferenceTest.scenarios, tests[0].scenarios) class TestLoadTests(testtools.TestCase): - class SampleTest(unittest.TestCase): - def test_nothing(self): + def test_nothing(self): pass + scenarios = [ - ('a', {}), - ('b', {}), - ] + ("a", {}), + ("b", {}), + ] def test_load_tests_apply_scenarios(self): suite = load_tests_apply_scenarios( - unittest.TestLoader(), - [self.SampleTest('test_nothing')], - None) + unittest.TestLoader(), [self.SampleTest("test_nothing")], None + ) result_tests = list(testtools.iterate_tests(suite)) - self.assertEquals( - 2, - len(result_tests), - result_tests) + self.assertEquals(2, len(result_tests), result_tests) def test_load_tests_apply_scenarios_old_style(self): """Call load_tests in the way used by bzr.""" suite = load_tests_apply_scenarios( - [self.SampleTest('test_nothing')], + [self.SampleTest("test_nothing")], self.__class__.__module__, unittest.TestLoader(), - ) + ) result_tests = list(testtools.iterate_tests(suite)) - self.assertEquals( - 2, - len(result_tests), - result_tests) + self.assertEquals(2, len(result_tests), result_tests) class TestMultiplyScenarios(testtools.TestCase): - def test_multiply_scenarios(self): def factory(name): - for i in 'ab': + for i in "ab": yield i, {name: i} - scenarios = multiply_scenarios(factory('p'), factory('q')) - self.assertEqual([ - ('a,a', dict(p='a', q='a')), - ('a,b', dict(p='a', q='b')), - ('b,a', dict(p='b', q='a')), - ('b,b', dict(p='b', q='b')), + + scenarios = multiply_scenarios(factory("p"), factory("q")) + self.assertEqual( + [ + ("a,a", dict(p="a", q="a")), + ("a,b", dict(p="a", q="b")), + ("b,a", dict(p="b", q="a")), + ("b,b", dict(p="b", q="b")), ], - scenarios) + scenarios, + ) def test_multiply_many_scenarios(self): def factory(name): - for i in 'abc': + for i in "abc": yield i, {name: i} - scenarios = multiply_scenarios(factory('p'), factory('q'), - factory('r'), factory('t')) - self.assertEqual( - 3**4, - len(scenarios), - scenarios) - self.assertEqual( - 'a,a,a,a', - scenarios[0][0]) + scenarios = multiply_scenarios( + factory("p"), factory("q"), factory("r"), factory("t") + ) + self.assertEqual(3**4, len(scenarios), scenarios) + self.assertEqual("a,a,a,a", scenarios[0][0]) -class TestPerModuleScenarios(testtools.TestCase): +class TestPerModuleScenarios(testtools.TestCase): def test_per_module_scenarios(self): """Generate scenarios for available modules""" s = testscenarios.scenarios.per_module_scenarios( - 'the_module', [ - ('Python', 'testscenarios'), - ('unittest', 'unittest'), - ('nonexistent', 'nonexistent'), - ]) - self.assertEqual('nonexistent', s[-1][0]) - self.assertIsInstance(s[-1][1]['the_module'], tuple) - s[-1][1]['the_module'] = None - self.assertEqual(s, [ - ('Python', {'the_module': testscenarios}), - ('unittest', {'the_module': unittest}), - ('nonexistent', {'the_module': None}), - ]) + "the_module", + [ + ("Python", "testscenarios"), + ("unittest", "unittest"), + ("nonexistent", "nonexistent"), + ], + ) + self.assertEqual("nonexistent", s[-1][0]) + self.assertIsInstance(s[-1][1]["the_module"], tuple) + s[-1][1]["the_module"] = None + self.assertEqual( + s, + [ + ("Python", {"the_module": testscenarios}), + ("unittest", {"the_module": unittest}), + ("nonexistent", {"the_module": None}), + ], + ) diff --git a/testscenarios/tests/test_testcase.py b/testscenarios/tests/test_testcase.py index 4860758..b1a54df 100644 --- a/testscenarios/tests/test_testcase.py +++ b/testscenarios/tests/test_testcase.py @@ -2,12 +2,12 @@ # dependency injection ('scenarios') by tests. # # Copyright (c) 2009, Robert Collins -# +# # Licensed under either the Apache License, Version 2.0 or the BSD 3-clause # license at the users choice. A copy of both licenses are available in the # project source as Apache-2.0 and BSD. You may not use this file except in # compliance with one of these two licences. -# +# # Unless required by applicable law or agreed to in writing, software # distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the @@ -22,23 +22,27 @@ import testscenarios -class TestTestWithScenarios(testtools.TestCase): +class TestTestWithScenarios(testtools.TestCase): scenarios = testscenarios.scenarios.per_module_scenarios( - 'impl', (('unittest', 'unittest'), ('unittest2', 'unittest2'))) + "impl", (("unittest", "unittest"), ("unittest2", "unittest2")) + ) @property def Implementation(self): if isinstance(self.impl, tuple): - self.skipTest('import failed - module not installed?') + self.skipTest("import failed - module not installed?") + class Implementation(testscenarios.WithScenarios, self.impl.TestCase): pass + return Implementation def test_no_scenarios_no_error(self): class ReferenceTest(self.Implementation): def test_pass(self): pass + test = ReferenceTest("test_pass") result = unittest.TestResult() test.run(result) @@ -47,41 +51,42 @@ def test_pass(self): def test_with_one_scenario_one_run(self): class ReferenceTest(self.Implementation): - scenarios = [('demo', {})] + scenarios = [("demo", {})] + def test_pass(self): pass + test = ReferenceTest("test_pass") log = [] result = LoggingResult(log) test.run(result) self.assertTrue(result.wasSuccessful()) self.assertEqual(1, result.testsRun) - self.expectThat( - log[0][1].id(), EndsWith('ReferenceTest.test_pass(demo)')) + self.expectThat(log[0][1].id(), EndsWith("ReferenceTest.test_pass(demo)")) def test_with_two_scenarios_two_run(self): class ReferenceTest(self.Implementation): - scenarios = [('1', {}), ('2', {})] + scenarios = [("1", {}), ("2", {})] + def test_pass(self): pass + test = ReferenceTest("test_pass") log = [] result = LoggingResult(log) test.run(result) self.assertTrue(result.wasSuccessful()) self.assertEqual(2, result.testsRun) - self.expectThat( - log[0][1].id(), EndsWith('ReferenceTest.test_pass(1)')) - self.expectThat( - log[4][1].id(), EndsWith('ReferenceTest.test_pass(2)')) + self.expectThat(log[0][1].id(), EndsWith("ReferenceTest.test_pass(1)")) + self.expectThat(log[4][1].id(), EndsWith("ReferenceTest.test_pass(2)")) def test_attributes_set(self): class ReferenceTest(self.Implementation): - scenarios = [ - ('1', {'foo': 1, 'bar': 2}), - ('2', {'foo': 2, 'bar': 4})] + scenarios = [("1", {"foo": 1, "bar": 2}), ("2", {"foo": 2, "bar": 4})] + def test_check_foo(self): self.assertEqual(self.foo * 2, self.bar) + test = ReferenceTest("test_check_foo") log = [] result = LoggingResult(log) @@ -91,11 +96,11 @@ def test_check_foo(self): def test_scenarios_attribute_cleared(self): class ReferenceTest(self.Implementation): - scenarios = [ - ('1', {'foo': 1, 'bar': 2}), - ('2', {'foo': 2, 'bar': 4})] + scenarios = [("1", {"foo": 1, "bar": 2}), ("2", {"foo": 2, "bar": 4})] + def test_check_foo(self): self.assertEqual(self.foo * 2, self.bar) + test = ReferenceTest("test_check_foo") log = [] result = LoggingResult(log) @@ -110,43 +115,49 @@ def test_countTestCases_no_scenarios(self): class ReferenceTest(self.Implementation): def test_check_foo(self): pass + test = ReferenceTest("test_check_foo") self.assertEqual(1, test.countTestCases()) def test_countTestCases_empty_scenarios(self): class ReferenceTest(self.Implementation): scenarios = [] + def test_check_foo(self): pass + test = ReferenceTest("test_check_foo") self.assertEqual(1, test.countTestCases()) def test_countTestCases_1_scenarios(self): class ReferenceTest(self.Implementation): - scenarios = [('1', {'foo': 1, 'bar': 2})] + scenarios = [("1", {"foo": 1, "bar": 2})] + def test_check_foo(self): pass + test = ReferenceTest("test_check_foo") self.assertEqual(1, test.countTestCases()) def test_countTestCases_2_scenarios(self): class ReferenceTest(self.Implementation): - scenarios = [ - ('1', {'foo': 1, 'bar': 2}), - ('2', {'foo': 2, 'bar': 4})] + scenarios = [("1", {"foo": 1, "bar": 2}), ("2", {"foo": 2, "bar": 4})] + def test_check_foo(self): pass + test = ReferenceTest("test_check_foo") self.assertEqual(2, test.countTestCases()) def test_debug_2_scenarios(self): log = [] + class ReferenceTest(self.Implementation): - scenarios = [ - ('1', {'foo': 1, 'bar': 2}), - ('2', {'foo': 2, 'bar': 4})] + scenarios = [("1", {"foo": 1, "bar": 2}), ("2", {"foo": 2, "bar": 4})] + def test_check_foo(self): log.append(self) + test = ReferenceTest("test_check_foo") test.debug() self.assertEqual(2, len(log))