/usr/lib/python2.7/dist-packages/nose2/plugins/loader/functions.py is in python-nose2 0.5.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | """
Load tests from test functions in modules.
This plugin responds to :func:`loadTestsFromModule` by adding test
cases for all test functions in the module to ``event.extraTests``. It
uses ``session.testMethodPrefix`` to find test functions.
Functions that are generators, have param lists, or take arguments
are not collected.
This plugin also implements :func:`loadTestsFromName` to enable loading
tests from dotted function names passed on the command line.
Fixtures
--------
Test functions can specify setup and teardown fixtures as attributes on the
function, for example:
.. code :: python
x = 0
def test():
assert x
def setup():
global x
x = 1
def teardown():
global x
x = 1
test.setup = setup
test.teardown = teardown
The setup attribute may be named ``setup``, ``setUp`` or ``setUpFunc``. The
teardown attribute may be named ``teardown``, 'tearDown`` or ``tearDownFunc``.
Other attributes
----------------
The other significant attribute that may be set on a test function is
``paramList``. When ``paramList`` is set, the function will be collected
by the :doc:`parameterized test loader <parameters>`. The easiest way
to set ``paramList`` is with the :func:`nose2.tools.params` decorator.
"""
# This module contains some code copied from unittest2/ and other code
# developed in reference to unittest2.
# unittest2 is Copyright (c) 2001-2010 Python Software Foundation; All
# Rights Reserved. See: http://docs.python.org/license.html
import inspect
import types
from nose2 import util
from nose2.events import Plugin
from nose2.compat import unittest
__unittest = True
class Functions(Plugin):
"""Loader plugin that loads test functions"""
alwaysOn = True
configSection = 'functions'
def registerInSubprocess(self, event):
event.pluginClasses.append(self.__class__)
def loadTestsFromName(self, event):
"""Load test if event.name is the name of a test function"""
name = event.name
module = event.module
try:
result = util.test_from_name(name, module)
except (AttributeError, ImportError) as e:
event.handled = True
return event.loader.failedLoadTests(name, e)
if result is None:
return
parent, obj, name, index = result
if (isinstance(obj, types.FunctionType) and not
util.isgenerator(obj) and not
hasattr(obj, 'paramList') and not
inspect.getargspec(obj).args):
suite = event.loader.suiteClass()
suite.addTests(self._createTests(obj))
event.handled = True
return suite
def loadTestsFromModule(self, event):
"""Load test functions from event.module"""
module = event.module
def is_test(obj):
if not obj.__name__.startswith(self.session.testMethodPrefix):
return False
if inspect.getargspec(obj).args:
return False
return True
tests = []
for name in dir(module):
obj = getattr(module, name)
if isinstance(obj, types.FunctionType) and is_test(obj):
tests.extend(self._createTests(obj))
event.extraTests.extend(tests)
def _createTests(self, obj):
if not hasattr(obj, 'setUp'):
if hasattr(obj, 'setup'):
obj.setUp = obj.setup
elif hasattr(obj, 'setUpFunc'):
obj.setUp = obj.setUpFunc
if not hasattr(obj, 'tearDown'):
if hasattr(obj, 'teardown'):
obj.tearDown = obj.teardown
elif hasattr(obj, 'tearDownFunc'):
obj.tearDown = obj.tearDownFunc
tests = []
args = {}
setUp = getattr(obj, 'setUp', None)
tearDown = getattr(obj, 'tearDown', None)
if setUp is not None:
args['setUp'] = setUp
if tearDown is not None:
args['tearDown'] = tearDown
paramList = getattr(obj, 'paramList', None)
isGenerator = util.isgenerator(obj)
if paramList is not None or isGenerator:
return tests
else:
case = util.transplant_class(
unittest.FunctionTestCase, obj.__module__)(obj, **args)
tests.append(case)
return tests
|