testing: clean up parametrization in test_mark.py (#7184)

This commit is contained in:
Ran Benita 2020-05-07 20:42:04 +03:00 committed by GitHub
parent 32c00db0bf
commit de556f895f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 40 additions and 45 deletions

View File

@ -197,17 +197,17 @@ def test_strict_prohibits_unregistered_markers(testdir, option_name):
@pytest.mark.parametrize( @pytest.mark.parametrize(
"spec", ("expr", "expected_passed"),
[ [
("xyz", ("test_one",)), ("xyz", ["test_one"]),
("((( xyz)) )", ("test_one",)), ("((( xyz)) )", ["test_one"]),
("not not xyz", ("test_one",)), ("not not xyz", ["test_one"]),
("xyz and xyz2", ()), ("xyz and xyz2", []),
("xyz2", ("test_two",)), ("xyz2", ["test_two"]),
("xyz or xyz2", ("test_one", "test_two")), ("xyz or xyz2", ["test_one", "test_two"]),
], ],
) )
def test_mark_option(spec, testdir): def test_mark_option(expr: str, expected_passed: str, testdir) -> None:
testdir.makepyfile( testdir.makepyfile(
""" """
import pytest import pytest
@ -219,18 +219,17 @@ def test_mark_option(spec, testdir):
pass pass
""" """
) )
opt, passed_result = spec rec = testdir.inline_run("-m", expr)
rec = testdir.inline_run("-m", opt)
passed, skipped, fail = rec.listoutcomes() passed, skipped, fail = rec.listoutcomes()
passed = [x.nodeid.split("::")[-1] for x in passed] passed = [x.nodeid.split("::")[-1] for x in passed]
assert len(passed) == len(passed_result) assert passed == expected_passed
assert list(passed) == list(passed_result)
@pytest.mark.parametrize( @pytest.mark.parametrize(
"spec", [("interface", ("test_interface",)), ("not interface", ("test_nointer",))] ("expr", "expected_passed"),
[("interface", ["test_interface"]), ("not interface", ["test_nointer"])],
) )
def test_mark_option_custom(spec, testdir): def test_mark_option_custom(expr: str, expected_passed: str, testdir) -> None:
testdir.makeconftest( testdir.makeconftest(
""" """
import pytest import pytest
@ -248,26 +247,25 @@ def test_mark_option_custom(spec, testdir):
pass pass
""" """
) )
opt, passed_result = spec rec = testdir.inline_run("-m", expr)
rec = testdir.inline_run("-m", opt)
passed, skipped, fail = rec.listoutcomes() passed, skipped, fail = rec.listoutcomes()
passed = [x.nodeid.split("::")[-1] for x in passed] passed = [x.nodeid.split("::")[-1] for x in passed]
assert len(passed) == len(passed_result) assert passed == expected_passed
assert list(passed) == list(passed_result)
@pytest.mark.parametrize( @pytest.mark.parametrize(
"spec", ("expr", "expected_passed"),
[ [
("interface", ("test_interface",)), ("interface", ["test_interface"]),
("not interface", ("test_nointer", "test_pass", "test_1", "test_2")), ("not interface", ["test_nointer", "test_pass", "test_1", "test_2"]),
("pass", ("test_pass",)), ("pass", ["test_pass"]),
("not pass", ("test_interface", "test_nointer", "test_1", "test_2")), ("not pass", ["test_interface", "test_nointer", "test_1", "test_2"]),
("not not not (pass)", ("test_interface", "test_nointer", "test_1", "test_2")), ("not not not (pass)", ["test_interface", "test_nointer", "test_1", "test_2"]),
("1 or 2", ("test_1", "test_2")), ("1 or 2", ["test_1", "test_2"]),
("not (1 or 2)", ["test_interface", "test_nointer", "test_pass"]),
], ],
) )
def test_keyword_option_custom(spec, testdir): def test_keyword_option_custom(expr: str, expected_passed: str, testdir) -> None:
testdir.makepyfile( testdir.makepyfile(
""" """
def test_interface(): def test_interface():
@ -282,12 +280,10 @@ def test_keyword_option_custom(spec, testdir):
pass pass
""" """
) )
opt, passed_result = spec rec = testdir.inline_run("-k", expr)
rec = testdir.inline_run("-k", opt)
passed, skipped, fail = rec.listoutcomes() passed, skipped, fail = rec.listoutcomes()
passed = [x.nodeid.split("::")[-1] for x in passed] passed = [x.nodeid.split("::")[-1] for x in passed]
assert len(passed) == len(passed_result) assert passed == expected_passed
assert list(passed) == list(passed_result)
def test_keyword_option_considers_mark(testdir): def test_keyword_option_considers_mark(testdir):
@ -298,14 +294,14 @@ def test_keyword_option_considers_mark(testdir):
@pytest.mark.parametrize( @pytest.mark.parametrize(
"spec", ("expr", "expected_passed"),
[ [
("None", ("test_func[None]",)), ("None", ["test_func[None]"]),
("[1.3]", ("test_func[1.3]",)), ("[1.3]", ["test_func[1.3]"]),
("2-3", ("test_func[2-3]",)), ("2-3", ["test_func[2-3]"]),
], ],
) )
def test_keyword_option_parametrize(spec, testdir): def test_keyword_option_parametrize(expr: str, expected_passed: str, testdir) -> None:
testdir.makepyfile( testdir.makepyfile(
""" """
import pytest import pytest
@ -314,12 +310,10 @@ def test_keyword_option_parametrize(spec, testdir):
pass pass
""" """
) )
opt, passed_result = spec rec = testdir.inline_run("-k", expr)
rec = testdir.inline_run("-k", opt)
passed, skipped, fail = rec.listoutcomes() passed, skipped, fail = rec.listoutcomes()
passed = [x.nodeid.split("::")[-1] for x in passed] passed = [x.nodeid.split("::")[-1] for x in passed]
assert len(passed) == len(passed_result) assert passed == expected_passed
assert list(passed) == list(passed_result)
def test_parametrize_with_module(testdir): def test_parametrize_with_module(testdir):
@ -338,7 +332,7 @@ def test_parametrize_with_module(testdir):
@pytest.mark.parametrize( @pytest.mark.parametrize(
"spec", ("expr", "expected_error"),
[ [
( (
"foo or", "foo or",
@ -360,17 +354,18 @@ def test_parametrize_with_module(testdir):
), ),
], ],
) )
def test_keyword_option_wrong_arguments(spec, testdir, capsys): def test_keyword_option_wrong_arguments(
expr: str, expected_error: str, testdir, capsys
) -> None:
testdir.makepyfile( testdir.makepyfile(
""" """
def test_func(arg): def test_func(arg):
pass pass
""" """
) )
opt, expected_result = spec testdir.inline_run("-k", expr)
testdir.inline_run("-k", opt) err = capsys.readouterr().err
out = capsys.readouterr().err assert expected_error in err
assert expected_result in out
def test_parametrized_collected_from_command_line(testdir): def test_parametrized_collected_from_command_line(testdir):