introduce a new "markers" attribute to nodes and the request object. It is

a dynamic class making holdin
This commit is contained in:
holger krekel 2012-09-17 17:32:23 +02:00
parent 646c2c6001
commit a94bb0a8bb
8 changed files with 127 additions and 70 deletions

View File

@ -1,6 +1,9 @@
Changes between 2.2.4 and 2.3.0.dev
-----------------------------------
- introduce a generic "markers" object on Nodes and request objects
to allow for reading and manipulation of MarkInfo objects previously
set with calls to pytest.mark.* classes.
- fix issue185 monkeypatching time.time does not cause pytest to fail
- fix issue172 duplicate call of pytest.setup-decoratored setup_module
functions

View File

@ -1,2 +1,2 @@
#
__version__ = '2.3.0.dev11'
__version__ = '2.3.0.dev12'

View File

@ -181,14 +181,21 @@ class Node(object):
#: filesystem path where this node was collected from (can be None)
self.fspath = getattr(parent, 'fspath', None)
#: keywords on this node (node name is always contained)
self.keywords = {self.name: True}
#: fspath sensitive hook proxy used to call pytest hooks
self.ihook = self.session.gethookproxy(self.fspath)
bases = parent and (parent.markers,) or ()
#: marker class with markers from all scopes accessible as attributes
self.markers = type("dynmarker", bases, {self.name: True})
#self.extrainit()
@property
def keywords(self):
""" dictionary of Keywords / markers on this node. """
return vars(self.markers)
#def extrainit(self):
# """"extra initialization after Node is initialized. Implemented
# by some subclasses. """

View File

@ -859,9 +859,11 @@ class Function(FunctionMixin, pytest.Item):
self.obj = callobj
startindex = int(self.cls is not None)
self.funcargnames = getfuncargnames(self.obj, startindex=startindex)
self.keywords.update(py.builtin._getfuncdict(self.obj) or {})
for name, val in (py.builtin._getfuncdict(self.obj) or {}).items():
setattr(self.markers, name, val)
if keywords:
self.keywords.update(keywords)
for name, val in keywords.items():
setattr(self.markers, name, val)
@property
def function(self):
@ -959,6 +961,10 @@ class FuncargRequest:
self.parentid = pyfuncitem.parent.nodeid
self._factorystack = []
@property
def markers(self):
return self._getscopeitem(self.scope).markers
def _getfaclist(self, argname):
facdeflist = self._name2factory.get(argname, None)
if facdeflist is None:
@ -1011,7 +1017,7 @@ class FuncargRequest:
@property
def keywords(self):
""" keywords of the test function item. """
""" dictionary of markers (readonly). """
return self._pyfuncitem.keywords
@property
@ -1019,8 +1025,6 @@ class FuncargRequest:
""" pytest session object. """
return self._pyfuncitem.session
def addfinalizer(self, finalizer):
"""add finalizer/teardown function to be called after the
last test within the requesting test context finished
@ -1047,7 +1051,8 @@ class FuncargRequest:
"""
if not isinstance(marker, py.test.mark.XYZ.__class__):
raise ValueError("%r is not a py.test.mark.* object")
self._pyfuncitem.keywords[marker.markname] = marker
setattr(self.markers, marker.markname, marker)
#self._pyfuncitem.keywords[marker.markname] = marker
def raiseerror(self, msg):
""" raise a FuncargLookupError with the given message. """

View File

@ -26,29 +26,29 @@ You can then restrict a test run to only run tests marked with ``webtest``::
$ py.test -v -m webtest
=========================== test session starts ============================
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev2 -- /home/hpk/venv/1/bin/python
cachedir: /home/hpk/tmp/doc-exec-305/.cache
plugins: xdist, bugzilla, cache, oejskit, pep8, cov
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev11 -- /home/hpk/venv/1/bin/python
cachedir: /home/hpk/tmp/doc-exec-426/.cache
plugins: xdist, bugzilla, cache, oejskit, cli, timeout, pep8, cov
collecting ... collected 2 items
test_server.py:3: test_send_http PASSED
=================== 1 tests deselected by "-m 'webtest'" ===================
================== 1 passed, 1 deselected in 0.02 seconds ==================
================== 1 passed, 1 deselected in 0.01 seconds ==================
Or the inverse, running all tests except the webtest ones::
$ py.test -v -m "not webtest"
=========================== test session starts ============================
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev2 -- /home/hpk/venv/1/bin/python
cachedir: /home/hpk/tmp/doc-exec-305/.cache
plugins: xdist, bugzilla, cache, oejskit, pep8, cov
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev11 -- /home/hpk/venv/1/bin/python
cachedir: /home/hpk/tmp/doc-exec-426/.cache
plugins: xdist, bugzilla, cache, oejskit, cli, timeout, pep8, cov
collecting ... collected 2 items
test_server.py:6: test_something_quick PASSED
================= 1 tests deselected by "-m 'not webtest'" =================
================== 1 passed, 1 deselected in 0.02 seconds ==================
================== 1 passed, 1 deselected in 0.01 seconds ==================
Registering markers
-------------------------------------
@ -69,6 +69,8 @@ You can ask which markers exist for your test suite - the list includes our just
$ py.test --markers
@pytest.mark.webtest: mark a test as a webtest.
@pytest.mark.timeout(timeout, method=None): Set a timeout and timeout method on just one test item. The first argument, *timeout*, is the timeout in seconds while the keyword, *method*, takes the same values as the --timeout_method option.
@pytest.mark.skipif(*conditions): skip the given test function if evaluation of all conditions has a True value. Evaluation happens within the module global context. Example: skipif('sys.platform == "win32"') skips the test if we are on the win32 platform.
@pytest.mark.xfail(*conditions, reason=None, run=True): mark the the test function as an expected failure. Optionally specify a reason and run=False if you don't even want to execute the test function. Any positional condition strings will be evaluated (like with skipif) and if one is False the marker will not be applied.
@ -147,8 +149,8 @@ the given argument::
$ py.test -k send_http # running with the above defined examples
=========================== test session starts ============================
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev2
plugins: xdist, bugzilla, cache, oejskit, pep8, cov
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev11
plugins: xdist, bugzilla, cache, oejskit, cli, timeout, pep8, cov
collecting ... collected 4 items
test_server.py .
@ -160,8 +162,8 @@ And you can also run all tests except the ones that match the keyword::
$ py.test -k-send_http
=========================== test session starts ============================
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev2
plugins: xdist, bugzilla, cache, oejskit, pep8, cov
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev11
plugins: xdist, bugzilla, cache, oejskit, cli, timeout, pep8, cov
collecting ... collected 4 items
test_mark_classlevel.py ..
@ -174,8 +176,8 @@ Or to only select the class::
$ py.test -kTestClass
=========================== test session starts ============================
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev2
plugins: xdist, bugzilla, cache, oejskit, pep8, cov
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev11
plugins: xdist, bugzilla, cache, oejskit, cli, timeout, pep8, cov
collecting ... collected 4 items
test_mark_classlevel.py ..
@ -208,10 +210,8 @@ specifies via named environments::
"env(name): mark test to run only on named environment")
def pytest_runtest_setup(item):
if not isinstance(item, pytest.Function):
return
if hasattr(item.obj, 'env'):
envmarker = getattr(item.obj, 'env')
envmarker = getattr(item.markers, 'env', None)
if envmarker is not None:
envname = envmarker.args[0]
if envname != item.config.option.env:
pytest.skip("test requires env %r" % envname)
@ -230,31 +230,33 @@ the test needs::
$ py.test -E stage2
=========================== test session starts ============================
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev2
plugins: xdist, bugzilla, cache, oejskit, pep8, cov
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev11
plugins: xdist, bugzilla, cache, oejskit, cli, timeout, pep8, cov
collecting ... collected 1 items
test_someenv.py s
======================== 1 skipped in 0.02 seconds =========================
======================== 1 skipped in 0.01 seconds =========================
and here is one that specifies exactly the environment needed::
$ py.test -E stage1
=========================== test session starts ============================
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev2
plugins: xdist, bugzilla, cache, oejskit, pep8, cov
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev11
plugins: xdist, bugzilla, cache, oejskit, cli, timeout, pep8, cov
collecting ... collected 1 items
test_someenv.py .
========================= 1 passed in 0.02 seconds =========================
========================= 1 passed in 0.01 seconds =========================
The ``--markers`` option always gives you a list of available markers::
$ py.test --markers
@pytest.mark.env(name): mark test to run only on named environment
@pytest.mark.timeout(timeout, method=None): Set a timeout and timeout method on just one test item. The first argument, *timeout*, is the timeout in seconds while the keyword, *method*, takes the same values as the --timeout_method option.
@pytest.mark.skipif(*conditions): skip the given test function if evaluation of all conditions has a True value. Evaluation happens within the module global context. Example: skipif('sys.platform == "win32"') skips the test if we are on the win32 platform.
@pytest.mark.xfail(*conditions, reason=None, run=True): mark the the test function as an expected failure. Optionally specify a reason and run=False if you don't even want to execute the test function. Any positional condition strings will be evaluated (like with skipif) and if one is False the marker will not be applied.
@ -293,7 +295,7 @@ test function. From a conftest file we can read it like this::
import sys
def pytest_runtest_setup(item):
g = getattr(item.obj, 'glob', None)
g = getattr(item.markers, "glob", None)
if g is not None:
for info in g:
print ("glob args=%s kwargs=%s" %(info.args, info.kwargs))
@ -307,7 +309,7 @@ Let's run this without capturing output and see what we get::
glob args=('class',) kwargs={'x': 2}
glob args=('module',) kwargs={'x': 1}
.
1 passed in 0.02 seconds
1 passed in 0.01 seconds
marking platform specific tests with pytest
--------------------------------------------------------------
@ -330,7 +332,7 @@ for your particular platform, you could use the following plugin::
def pytest_runtest_setup(item):
if isinstance(item, item.Function):
plat = sys.platform
if not hasattr(item.obj, plat):
if not hasattr(item.markers, plat):
if ALL.intersection(set(item.obj.__dict__)):
pytest.skip("cannot run on platform %s" %(plat))
@ -360,13 +362,13 @@ then you will see two test skipped and two executed tests as expected::
$ py.test -rs # this option reports skip reasons
=========================== test session starts ============================
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev2
plugins: xdist, bugzilla, cache, oejskit, pep8, cov
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev11
plugins: xdist, bugzilla, cache, oejskit, cli, timeout, pep8, cov
collecting ... collected 4 items
test_plat.py s.s.
========================= short test summary info ==========================
SKIP [2] /home/hpk/tmp/doc-exec-305/conftest.py:12: cannot run on platform linux2
SKIP [2] /home/hpk/tmp/doc-exec-426/conftest.py:12: cannot run on platform linux2
=================== 2 passed, 2 skipped in 0.02 seconds ====================
@ -374,13 +376,13 @@ Note that if you specify a platform via the marker-command line option like this
$ py.test -m linux2
=========================== test session starts ============================
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev2
plugins: xdist, bugzilla, cache, oejskit, pep8, cov
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev11
plugins: xdist, bugzilla, cache, oejskit, cli, timeout, pep8, cov
collecting ... collected 4 items
test_plat.py .
=================== 3 tests deselected by "-m 'linux2'" ====================
================== 1 passed, 3 deselected in 0.02 seconds ==================
================== 1 passed, 3 deselected in 0.01 seconds ==================
then the unmarked-tests will not be run. It is thus a way to restrict the run to the specific tests.

View File

@ -24,7 +24,7 @@ def main():
name='pytest',
description='py.test: simple powerful testing with Python',
long_description = long_description,
version='2.3.0.dev11',
version='2.3.0.dev12',
url='http://pytest.org',
license='MIT license',
platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],

View File

@ -318,7 +318,7 @@ class TestKeywordSelection:
def pytest_pycollect_makeitem(__multicall__, name):
if name == "TestClass":
item = __multicall__.execute()
item.keywords['xxx'] = True
item.markers.xxx = True
return item
""")
for keyword in ('xxx', 'xxx test_2', 'TestClass', 'xxx -test_1',

View File

@ -609,7 +609,7 @@ class TestRequest:
""")
req = funcargs.FuncargRequest(item)
assert req.function == item.obj
assert req.keywords is item.keywords
assert req.keywords == item.keywords
assert hasattr(req.module, 'test_func')
assert req.cls is None
assert req.function.__name__ == "test_func"
@ -716,7 +716,8 @@ class TestRequest:
req = funcargs.FuncargRequest(item)
assert req.fspath == modcol.fspath
def test_applymarker(testdir):
class TestMarking:
def test_applymarker(self, testdir):
item1,item2 = testdir.getitems("""
def pytest_funcarg__something(request):
pass
@ -735,6 +736,44 @@ def test_applymarker(testdir):
assert 'skipif' in item1.keywords
pytest.raises(ValueError, "req1.applymarker(42)")
def test_accessmarker_function(self, testdir):
testdir.makepyfile("""
import pytest
@pytest.factory()
def markers(request):
return request.markers
@pytest.mark.XYZ
def test_function(markers):
assert markers.XYZ is not None
assert not hasattr(markers, "abc")
""")
reprec = testdir.inline_run()
reprec.assertoutcome(passed=1)
def test_accessmarker_dynamic(self, testdir):
testdir.makeconftest("""
import pytest
@pytest.factory()
def markers(request):
return request.markers
@pytest.setup(scope="class")
def marking(request):
request.applymarker(pytest.mark.XYZ("hello"))
""")
testdir.makepyfile("""
import pytest
def test_fun1(markers):
assert markers.XYZ is not None
assert not hasattr(markers, "abc")
def test_fun2(markers):
assert markers.XYZ is not None
assert not hasattr(markers, "abc")
""")
reprec = testdir.inline_run()
reprec.assertoutcome(passed=2)
class TestRequestCachedSetup:
def test_request_cachedsetup_defaultmodule(self, testdir):
reprec = testdir.inline_runsource("""
@ -2365,13 +2404,14 @@ class TestFuncargMarker:
reprec = testdir.inline_run("-v")
reprec.assertoutcome(passed=6)
@pytest.mark.parametrize(("scope", "ok", "error"),[
class TestTestContextScopeAccess:
pytestmark = pytest.mark.parametrize(("scope", "ok", "error"),[
["session", "", "fspath class function module"],
["module", "module fspath", "cls function"],
["class", "module fspath cls", "function"],
["function", "module fspath cls function", ""]
])
class TestTestContextScopeAccess:
def test_setup(self, testdir, scope, ok, error):
testdir.makepyfile("""
import pytest