decorators don't play nice with nose?

J Kenneth King james at agentultra.com
Mon Apr 6 10:08:03 EDT 2009


hyperboreean <hyperboreean at nerdshack.com> writes:

> From: hyperboreean <hyperboreean at nerdshack.com>
> Subject: decorators don't play nice with nose?
> Newsgroups: comp.lang.python
> To: python-list at python.org
> Date: Mon, 06 Apr 2009 11:01:04 +0300
>
> Hi, I am trying to test the business part of a web service. For this I
> am using unittest & nose.
> I wrote a decorator that should handle the xml test file retrieval,
> but it seems I can't get it working with nose.
> Here's the code:
>
>
> * MyApp.py -- base test class *
>
> import os
> import unittest
>
> from MyApp.Core import XmlParser
>
>
> __all__ = ['MyAppTest', 'setup']
>
>
> PATH = os.path.dirname(__file__) or ''
>
>
> class setup(object):
>    """Decorator to ease the use of xml files in MyApp tests.
>
>    The way it works it that it decorates a test method which has a first
>    default parameter called 'parser' and it overwrites this parameter value
>    with a XmlParser instance.
>
>    The xml file should be located under:
>    data/testedBusinessRequest/testMethodName.xml
>    """
>    def __init__(self, testedBusinessRequest = ''):
>        self.testedBusinessRequest =\
>                testedBusinessRequest.lower()
>
>
>    def _getXmlParser(self, xml):
>        documentElement = XmlParser.parseXmlStream(xml)
>        parser = XmlParser.getParser(documentElement)
>        return parser
>
>
>    def __call__(self, method):
>
>        # TODO: error handling here
>        methodName = method.func_code.co_name
>        methodName = methodName.split('_')[1]
>
>        xmlFolder = self.testedBusinessRequest
>        xmlFile = '%s.xml' % methodName
>
>        path = os.path.join(PATH, 'data',
>                xmlFolder, xmlFile)
>
>        f = open(path)
>        xml = f.read()
>        f.close()
>        method.func_defaults = (self._getXmlParser(xml),)
>        return method
>
>
> class MyAppTest(unittest.TestCase):
>
>    def setUp(self):
>        self.database = Database()
>
>    def tearDown(self):
>        pass
>
>
> * test_Login.py - test a business request *
> from MyAppTest import MyAppTest, setup
>
> from MyApp import Login
>
>
> class TestLogin(MyAppTest):
>    testedBusinessRequest = 'Login'
>
>    @setup(testedBusinessRequest)
>    def test_validParameters(self, parser = None):

FWIW, nose and unittest both provide methods for setting up and
tearing down tests. You should probably look at those first before
rolling your own. At the very least it will give you an idea of how
yours should work.

Cheers



More information about the Python-list mailing list