Skip to content

Commit 50d80c1

Browse files
author
jakubo
committed
Test file run twice fails if it contains marked class pytest-dev#683
1 parent ae28e4b commit 50d80c1

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

‎_pytest/python.py‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,19 @@ def __init__(self, argnames, names_closure, name2fixturedefs):
480480
self.names_closure = names_closure
481481
self.name2fixturedefs = name2fixturedefs
482482

483+
484+
def _marked(func, mark):
485+
"""Returns True if :func: is already marked with :mark:, False orherwise.
486+
This can happen if marker is applied to class and the test file is
487+
invoked more than once.
488+
"""
489+
try:
490+
func_mark = getattr(func, mark.name)
491+
except AttributeError:
492+
return False
493+
return mark.args == func_mark.args and mark.kwargs == func_mark.kwargs
494+
495+
483496
def transfer_markers(funcobj, cls, mod):
484497
# XXX this should rather be code in the mark plugin or the mark
485498
# plugin should merge with the python plugin.
@@ -490,9 +503,11 @@ def transfer_markers(funcobj, cls, mod):
490503
continue
491504
if isinstance(pytestmark, list):
492505
for mark in pytestmark:
493-
mark(funcobj)
506+
if not _marked(funcobj, mark):
507+
mark(funcobj)
494508
else:
495-
pytestmark(funcobj)
509+
if not _marked(funcobj, pytestmark):
510+
pytestmark(funcobj)
496511

497512
class Module(pytest.File, PyCollector):
498513
""" Collector for test classes and functions. """

‎testing/test_mark.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
import py, pytest
24
from _pytest.mark import MarkGenerator as Mark
35

@@ -84,6 +86,22 @@ def g():
8486
assert g.some.kwargs['reason2'] == "456"
8587

8688

89+
def test_marked_class_run_twice(testdir, request):
90+
"""Test fails file is run twice that contains marked class.
91+
See issue#683.
92+
"""
93+
py_file = testdir.makepyfile("""
94+
import pytest
95+
@pytest.mark.parametrize('abc', [1, 2, 3])
96+
class Test1(object):
97+
def test_1(self, abc):
98+
assert abc in [1, 2, 3]
99+
""")
100+
file_name = os.path.basename(py_file.strpath)
101+
rec = testdir.inline_run(file_name, file_name)
102+
rec.assertoutcome(passed=6)
103+
104+
87105
def test_ini_markers(testdir):
88106
testdir.makeini("""
89107
[pytest]

0 commit comments

Comments
 (0)