Back to index

moin  1.9.0~rc2
conftest.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003 MoinMoin Testing Framework
00004 --------------------------
00005 
00006 All test modules must be named test_modulename to be included in the
00007 test suite. If you are testing a package, name the test module
00008 test_package_module.
00009 
00010 Tests that need the current request, for example to create a page
00011 instance, can refer to self.request. It is injected into all test case
00012 classes by the framework.
00013 
00014 Tests that require a certain configuration, like section_numbers = 1, must
00015 use a Config class to define the required configuration within the test class.
00016 
00017 @copyright: 2005 MoinMoin:NirSoffer,
00018             2007 MoinMoin:AlexanderSchremmer,
00019             2008 MoinMoin:ThomasWaldmann
00020 @license: GNU GPL, see COPYING for details.
00021 """
00022 
00023 import atexit
00024 import sys
00025 
00026 import py
00027 
00028 rootdir = py.magic.autopath().dirpath()
00029 moindir = rootdir.join("..")
00030 sys.path.insert(0, str(moindir))
00031 
00032 from MoinMoin.support.python_compatibility import set
00033 from MoinMoin.web.request import TestRequest, Client
00034 from MoinMoin.wsgiapp import Application, init
00035 from MoinMoin._tests import maketestwiki, wikiconfig
00036 
00037 coverage_modules = set()
00038 
00039 try:
00040     """
00041     This code adds support for coverage.py (see
00042     http://nedbatchelder.com/code/modules/coverage.html).
00043     It prints a coverage report for the modules specified in all
00044     module globals (of the test modules) named "coverage_modules".
00045     """
00046 
00047     import coverage
00048 
00049     def report_coverage():
00050         coverage.stop()
00051         module_list = [sys.modules[mod] for mod in coverage_modules]
00052         module_list.sort()
00053         coverage.report(module_list)
00054 
00055     def callback(option, opt_str, value, parser):
00056         atexit.register(report_coverage)
00057         coverage.erase()
00058         coverage.start()
00059 
00060     py.test.config.addoptions('MoinMoin options', py.test.config.Option('-C',
00061         '--coverage', action='callback', callback=callback,
00062         help='Output information about code coverage (slow!)'))
00063 
00064 except ImportError:
00065     coverage = None
00066 
00067 
00068 def init_test_request(given_config=None, static_state=[False]):
00069     if not static_state[0]:
00070         maketestwiki.run(True)
00071         static_state[0] = True
00072     request = TestRequest()
00073     request.given_config = given_config
00074     request = init(request)
00075     return request
00076 
00077 
00078 # py.test customization starts here
00079 
00080 # py.test-1.0 provides "funcargs" natively
00081 def pytest_funcarg__request(request):
00082     # note the naminng clash: py.test's funcarg-request object
00083     # and the request we provide are totally separate things
00084     cls = request._pyfuncitem.getparent(py.test.collect.Module)
00085     return cls.request
00086 
00087 class MoinTestFunction(py.test.collect.Function):
00088     def execute(self, target, *args):
00089         request = self.parent.request
00090         co = target.func_code
00091         if 'request' in co.co_varnames[:co.co_argcount]:
00092             target(request, *args)
00093         else:
00094             target(*args)
00095 
00096 
00097 class MoinClassCollector(py.test.collect.Class):
00098     Function = MoinTestFunction
00099 
00100     def setup(self):
00101         cls = self.obj
00102         if hasattr(cls, 'Config'):
00103             cls.request = init_test_request(given_config=cls.Config)
00104             cls.client = Client(Application(cls.Config))
00105         else:
00106             cls.request = self.parent.request
00107             #XXX: this is the extremely messy way to configure the wsgi app
00108             #     with the correct testing config
00109             cls.client = Client(Application(self.parent.request.cfg.__class__))
00110         super(MoinClassCollector, self).setup()
00111 
00112 
00113 class Module(py.test.collect.Module):
00114     Class = MoinClassCollector
00115     Function = MoinTestFunction
00116 
00117     def __init__(self, *args, **kwargs):
00118         self.request = init_test_request(given_config=wikiconfig.Config)
00119         super(Module, self).__init__(*args, **kwargs)
00120 
00121     def run(self, *args, **kwargs):
00122         if coverage is not None:
00123             coverage_modules.update(getattr(self.obj, 'coverage_modules', []))
00124         return super(Module, self).run(*args, **kwargs)