From 40b85d7ee89388245e7bb45b044993251c71664c Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sat, 1 Dec 2018 16:41:59 -0200 Subject: [PATCH] Remove Metafunc.addcall Fix #3083 --- changelog/3083.removal.rst | 3 + doc/en/deprecations.rst | 31 +++++-- src/_pytest/deprecated.py | 5 -- src/_pytest/fixtures.py | 3 +- src/_pytest/python.py | 42 ---------- testing/acceptance_test.py | 5 +- testing/deprecated_test.py | 17 ---- testing/python/metafunc.py | 164 ++----------------------------------- 8 files changed, 37 insertions(+), 233 deletions(-) create mode 100644 changelog/3083.removal.rst diff --git a/changelog/3083.removal.rst b/changelog/3083.removal.rst new file mode 100644 index 000000000..ce689b94a --- /dev/null +++ b/changelog/3083.removal.rst @@ -0,0 +1,3 @@ +Remove ``Metafunc.addcall``. This was the predecessor mechanism to ``@pytest.mark.parametrize``. + +See our `docs `__ on information on how to update your code. diff --git a/doc/en/deprecations.rst b/doc/en/deprecations.rst index b6ec4f245..5f57b51d2 100644 --- a/doc/en/deprecations.rst +++ b/doc/en/deprecations.rst @@ -168,13 +168,6 @@ Defining ``pytest_plugins`` is now deprecated in non-top-level conftest.py files because they will activate referenced plugins *globally*, which is surprising because for all other pytest features ``conftest.py`` files are only *active* for tests at or below it. -Metafunc.addcall -~~~~~~~~~~~~~~~~ - -.. deprecated:: 3.3 - -:meth:`_pytest.python.Metafunc.addcall` was a precursor to the current parametrized mechanism. Users should use -:meth:`_pytest.python.Metafunc.parametrize` instead. marks in ``pytest.mark.parametrize`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -274,6 +267,30 @@ Removed Features As stated in our :ref:`backwards-compatibility` policy, deprecated features are removed only in major releases after an appropriate period of deprecation has passed. +Metafunc.addcall +~~~~~~~~~~~~~~~~ + +*Removed in version 4.0.* + +:meth:`_pytest.python.Metafunc.addcall` was a precursor to the current parametrized mechanism. Users should use +:meth:`_pytest.python.Metafunc.parametrize` instead. + +Example: + +.. code-block:: python + + def pytest_generate_tests(metafunc): + metafunc.addcall({"i": 1}, id="1") + metafunc.addcall({"i": 2}, id="2") + +Becomes: + +.. code-block:: python + + def pytest_generate_tests(metafunc): + metafunc.parametrize("i", [1, 2], ids=["1", "2"]) + + ``cached_setup`` ~~~~~~~~~~~~~~~~ diff --git a/src/_pytest/deprecated.py b/src/_pytest/deprecated.py index 94ba6d037..00e066ff0 100644 --- a/src/_pytest/deprecated.py +++ b/src/_pytest/deprecated.py @@ -91,11 +91,6 @@ COLLECTOR_MAKEITEM = RemovedInPytest4Warning( "pycollector makeitem was removed as it is an accidentially leaked internal api" ) -METAFUNC_ADD_CALL = RemovedInPytest4Warning( - "Metafunc.addcall is deprecated and scheduled to be removed in pytest 4.0.\n" - "Please use Metafunc.parametrize instead." -) - PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST = RemovedInPytest4Warning( "Defining pytest_plugins in a non-top-level conftest is deprecated, " "because it affects the entire directory tree in a non-explicit way.\n" diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 1519d2fbc..3e5ad5f73 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -568,8 +568,7 @@ class FixtureRequest(FuncargnamesCompatAttr): ) fail(msg, pytrace=False) else: - # indices might not be set if old-style metafunc.addcall() was used - param_index = funcitem.callspec.indices.get(argname, 0) + param_index = funcitem.callspec.indices[argname] # if a parametrize invocation set a scope it will override # the static scope defined with the fixture function paramscopenum = funcitem.callspec._arg2scopenum.get(argname) diff --git a/src/_pytest/python.py b/src/_pytest/python.py index 7ddcc2762..3db36fb19 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -1022,48 +1022,6 @@ class Metafunc(fixtures.FuncargnamesCompatAttr): pytrace=False, ) - def addcall(self, funcargs=None, id=NOTSET, param=NOTSET): - """ Add a new call to the underlying test function during the collection phase of a test run. - - .. deprecated:: 3.3 - - Use :meth:`parametrize` instead. - - Note that request.addcall() is called during the test collection phase prior and - independently to actual test execution. You should only use addcall() - if you need to specify multiple arguments of a test function. - - :arg funcargs: argument keyword dictionary used when invoking - the test function. - - :arg id: used for reporting and identification purposes. If you - don't supply an `id` an automatic unique id will be generated. - - :arg param: a parameter which will be exposed to a later fixture function - invocation through the ``request.param`` attribute. - """ - warnings.warn(deprecated.METAFUNC_ADD_CALL, stacklevel=2) - - assert funcargs is None or isinstance(funcargs, dict) - if funcargs is not None: - for name in funcargs: - if name not in self.fixturenames: - fail("funcarg %r not used in this function." % name) - else: - funcargs = {} - if id is None: - raise ValueError("id=None not allowed") - if id is NOTSET: - id = len(self._calls) - id = str(id) - if id in self._ids: - raise ValueError("duplicate id %r" % id) - self._ids.add(id) - - cs = CallSpec2(self) - cs.setall(funcargs, id, param) - self._calls.append(cs) - def _find_parametrized_scope(argnames, arg2fixturedefs, indirect): """Find the most appropriate scope for a parametrized call based on its arguments. diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index b23cd7ca8..0b7af5338 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -299,7 +299,7 @@ class TestGeneralUsage(object): """ import pytest def pytest_generate_tests(metafunc): - metafunc.addcall({'x': 3}, id='hello-123') + metafunc.parametrize('x', [3], ids=['hello-123']) def pytest_runtest_setup(item): print(item.keywords) if 'hello-123' in item.keywords: @@ -316,8 +316,7 @@ class TestGeneralUsage(object): p = testdir.makepyfile( """ def pytest_generate_tests(metafunc): - metafunc.addcall({'i': 1}, id="1") - metafunc.addcall({'i': 2}, id="2") + metafunc.parametrize('i', [1, 2], ids=["1", "2"]) def test_func(i): pass """ diff --git a/testing/deprecated_test.py b/testing/deprecated_test.py index aa3c5e58b..71ea60050 100644 --- a/testing/deprecated_test.py +++ b/testing/deprecated_test.py @@ -105,23 +105,6 @@ def test_resultlog_is_deprecated(testdir): ) -def test_metafunc_addcall_deprecated(testdir): - testdir.makepyfile( - """ - def pytest_generate_tests(metafunc): - metafunc.addcall({'i': 1}) - metafunc.addcall({'i': 2}) - def test_func(i): - pass - """ - ) - res = testdir.runpytest("-s", SHOW_PYTEST_WARNINGS_ARG) - assert res.ret == 0 - res.stdout.fnmatch_lines( - ["*Metafunc.addcall is deprecated*", "*2 passed, 2 warnings*"] - ) - - def test_terminal_reporter_writer_attr(pytestconfig): """Check that TerminalReporter._tw is also available as 'writer' (#2984) This attribute is planned to be deprecated in 3.4. diff --git a/testing/python/metafunc.py b/testing/python/metafunc.py index 833eb5641..7f9cdb5cc 100644 --- a/testing/python/metafunc.py +++ b/testing/python/metafunc.py @@ -54,66 +54,6 @@ class TestMetafunc(object): assert metafunc.function is func assert metafunc.cls is None - def test_addcall_no_args(self): - def func(arg1): - pass - - metafunc = self.Metafunc(func) - metafunc.addcall() - assert len(metafunc._calls) == 1 - call = metafunc._calls[0] - assert call.id == "0" - assert not hasattr(call, "param") - - def test_addcall_id(self): - def func(arg1): - pass - - metafunc = self.Metafunc(func) - pytest.raises(ValueError, metafunc.addcall, id=None) - - metafunc.addcall(id=1) - pytest.raises(ValueError, metafunc.addcall, id=1) - pytest.raises(ValueError, metafunc.addcall, id="1") - metafunc.addcall(id=2) - assert len(metafunc._calls) == 2 - assert metafunc._calls[0].id == "1" - assert metafunc._calls[1].id == "2" - - def test_addcall_param(self): - def func(arg1): - pass - - metafunc = self.Metafunc(func) - - class obj(object): - pass - - metafunc.addcall(param=obj) - metafunc.addcall(param=obj) - metafunc.addcall(param=1) - assert len(metafunc._calls) == 3 - assert metafunc._calls[0].getparam("arg1") == obj - assert metafunc._calls[1].getparam("arg1") == obj - assert metafunc._calls[2].getparam("arg1") == 1 - - def test_addcall_funcargs(self): - def func(x): - pass - - metafunc = self.Metafunc(func) - - class obj(object): - pass - - metafunc.addcall(funcargs={"x": 2}) - metafunc.addcall(funcargs={"x": 3}) - pytest.raises(pytest.fail.Exception, metafunc.addcall, {"xyz": 0}) - assert len(metafunc._calls) == 2 - assert metafunc._calls[0].funcargs == {"x": 2} - assert metafunc._calls[1].funcargs == {"x": 3} - assert not hasattr(metafunc._calls[1], "param") - def test_parametrize_error(self): def func(x, y): pass @@ -508,19 +448,6 @@ class TestMetafunc(object): ) assert result == ["a0", "a1", "b0", "c", "b1"] - def test_addcall_and_parametrize(self): - def func(x, y): - pass - - metafunc = self.Metafunc(func) - metafunc.addcall({"x": 1}) - metafunc.parametrize("y", [2, 3]) - assert len(metafunc._calls) == 2 - assert metafunc._calls[0].funcargs == {"x": 1, "y": 2} - assert metafunc._calls[1].funcargs == {"x": 1, "y": 3} - assert metafunc._calls[0].id == "0-2" - assert metafunc._calls[1].id == "0-3" - @pytest.mark.issue714 def test_parametrize_indirect(self): def func(x, y): @@ -710,20 +637,6 @@ class TestMetafunc(object): ["*already takes an argument 'y' with a default value"] ) - def test_addcalls_and_parametrize_indirect(self): - def func(x, y): - pass - - metafunc = self.Metafunc(func) - metafunc.addcall(param="123") - metafunc.parametrize("x", [1], indirect=True) - metafunc.parametrize("y", [2, 3], indirect=True) - assert len(metafunc._calls) == 2 - assert metafunc._calls[0].funcargs == {} - assert metafunc._calls[1].funcargs == {} - assert metafunc._calls[0].params == dict(x=1, y=2) - assert metafunc._calls[1].params == dict(x=1, y=3) - def test_parametrize_functional(self, testdir): testdir.makepyfile( """ @@ -871,7 +784,7 @@ class TestMetafuncFunctional(object): # assumes that generate/provide runs in the same process import sys, pytest, six def pytest_generate_tests(metafunc): - metafunc.addcall(param=metafunc) + metafunc.parametrize('metafunc', [metafunc]) @pytest.fixture def metafunc(request): @@ -896,43 +809,15 @@ class TestMetafuncFunctional(object): result = testdir.runpytest(p, "-v", SHOW_PYTEST_WARNINGS_ARG) result.assert_outcomes(passed=2) - def test_addcall_with_two_funcargs_generators(self, testdir): - testdir.makeconftest( - """ - def pytest_generate_tests(metafunc): - assert "arg1" in metafunc.fixturenames - metafunc.addcall(funcargs=dict(arg1=1, arg2=2)) - """ - ) - p = testdir.makepyfile( - """ - def pytest_generate_tests(metafunc): - metafunc.addcall(funcargs=dict(arg1=1, arg2=1)) - - class TestClass(object): - def test_myfunc(self, arg1, arg2): - assert arg1 == arg2 - """ - ) - result = testdir.runpytest("-v", p, SHOW_PYTEST_WARNINGS_ARG) - result.stdout.fnmatch_lines( - ["*test_myfunc*0*PASS*", "*test_myfunc*1*FAIL*", "*1 failed, 1 passed*"] - ) - def test_two_functions(self, testdir): p = testdir.makepyfile( """ def pytest_generate_tests(metafunc): - metafunc.addcall(param=10) - metafunc.addcall(param=20) - - import pytest - @pytest.fixture - def arg1(request): - return request.param + metafunc.parametrize('arg1', [10, 20], ids=['0', '1']) def test_func1(arg1): assert arg1 == 10 + def test_func2(arg1): assert arg1 in (10, 20) """ @@ -943,6 +828,7 @@ class TestMetafuncFunctional(object): "*test_func1*0*PASS*", "*test_func1*1*FAIL*", "*test_func2*PASS*", + "*test_func2*PASS*", "*1 failed, 3 passed*", ] ) @@ -961,47 +847,12 @@ class TestMetafuncFunctional(object): result = testdir.runpytest(p) result.assert_outcomes(passed=1) - def test_generate_plugin_and_module(self, testdir): - testdir.makeconftest( - """ - def pytest_generate_tests(metafunc): - assert "arg1" in metafunc.fixturenames - metafunc.addcall(id="world", param=(2,100)) - """ - ) - p = testdir.makepyfile( - """ - def pytest_generate_tests(metafunc): - metafunc.addcall(param=(1,1), id="hello") - - import pytest - @pytest.fixture - def arg1(request): - return request.param[0] - @pytest.fixture - def arg2(request): - return request.param[1] - - class TestClass(object): - def test_myfunc(self, arg1, arg2): - assert arg1 == arg2 - """ - ) - result = testdir.runpytest("-v", p, SHOW_PYTEST_WARNINGS_ARG) - result.stdout.fnmatch_lines( - [ - "*test_myfunc*hello*PASS*", - "*test_myfunc*world*FAIL*", - "*1 failed, 1 passed*", - ] - ) - def test_generate_tests_in_class(self, testdir): p = testdir.makepyfile( """ class TestClass(object): def pytest_generate_tests(self, metafunc): - metafunc.addcall(funcargs={'hello': 'world'}, id="hello") + metafunc.parametrize('hello', ['world'], ids=['hellow']) def test_myfunc(self, hello): assert hello == "world" @@ -1014,8 +865,7 @@ class TestMetafuncFunctional(object): p = testdir.makepyfile( """ def pytest_generate_tests(metafunc): - metafunc.addcall({'arg1': 10}) - metafunc.addcall({'arg1': 20}) + metafunc.parametrize('arg1', [10, 20], ids=["0", "1"]) class TestClass(object): def test_func(self, arg1): @@ -1032,7 +882,7 @@ class TestMetafuncFunctional(object): p = testdir.makepyfile( """ def pytest_generate_tests(metafunc): - metafunc.addcall({'arg1': 1}) + metafunc.parametrize('arg1', [1]) class TestClass(object): def test_method(self, arg1):