Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8531147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:25:02+00:00 2026-06-11T09:25:02+00:00

I’ve been working on a way to get tests produced from a generator in

  • 0

I’ve been working on a way to get tests produced from a generator in nose to have descriptions that are customized for the specific iteration being tested. I have something that works, as long as my generator target method never tries to access self from my generator class. I’m seeing that all my generator target instances have a common test class instance while nose is generating a one-offed instance of the test class for each test run from the generator. This is resulting in setUp being run on each test instance nose creates, but never running on the instance the generator target is bound to (of course, the real problem is that I can’t see how to bind the nose-created instance to the generator target). Here’s the code I’m using to try to figure this all out (yes, I know the decorator would probably be better as a callable class, but nose, at least version 1.2.1 that I have, explicitly checks that tests are either functions or methods, so a callable class won’t run at all):

import inspect

def labelable_yielded_case(case):

    argspec = inspect.getargspec(case)
    if argspec.defaults is not None:
        defaults_list = [''] * (len(argspec.args) - len(argspec.defaults)) + argspec.defaults
    else:
        defaults_list = [''] * len(argspec.args)
    argument_defaults_list = zip(argspec.args, defaults_list)
    case_wrappers = []

    def add_description(wrapper_id, argument_dict):

        case_wrappers[wrapper_id].description = case.__doc__.format(**argument_dict)

    def case_factory(*factory_args, **factory_kwargs):

        def case_wrapper_wrapper():

            wrapper_id = len(case_wrappers)

            def case_wrapper(*args, **kwargs):

                args = factory_args + args
                argument_list = []
                for argument in argument_defaults_list:
                    argument_list.append(list(argument))
                for index, value in enumerate(args):
                    argument_list[index][1] = value
                argument_dict = dict(argument_list)
                argument_dict.update(factory_kwargs)
                argument_dict.update(kwargs)
                add_description(wrapper_id, argument_dict)
                return case(*args, **kwargs)

            case_wrappers.append(case_wrapper)
            case_wrapper.__name__ = case.__name__
            return case_wrapper

        return case_wrapper_wrapper()

    return case_factory


class TestTest(object):

    def __init__(self):

        self.data = None

    def setUp(self):

        print 'setup', self
        self.data = (1,2,3)

    def test_all(self):

        for index, value in enumerate((1,2,3)):
            yield self.validate_equality(), index, value

    def test_all_again(self):

        for index, value in enumerate((1,2,3)):
            yield self.validate_equality_again, index, value

    @labelable_yielded_case
    def validate_equality(self, index, value):
        '''element {index} equals {value}'''

        print 'test', self
        assert self.data[index] == value, 'expected %d got %d' % (value, self.data[index])

    def validate_equality_again(self, index, value):

        print 'test', self
        assert self.data[index] == value, 'expected %d got %d' % (value, self.data[index])

    validate_equality_again.description = 'again'

When run through nose, the again tests work just fine, but the set of tests using the decorated generator target all fail because self.data is None (because setUp is never run because the instance of TestTest stored in the closures is not the instances run by nose). I tried making the decorator an instance member of a base class for TestTest, but then nose threw errors about having too few arguments (no self) passed to the unbound labelable_yielded_case. Is there any way I can make this work (short of hacking nose), or am I stuck choosing between either not being able to have the yield target be an instance member or not having per-test labeling for each yielded test?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:25:03+00:00Added an answer on June 11, 2026 at 9:25 am

    Fixed it (at least for the case here, though I think I got it for all cases). I had to fiddle with case_wrapper_wrapper and case_wrapper to get the factory to return the wrapped cases attached to the correct class, but not bound to any given instance in any way. I also had another code issue because I was building the argument dict in wrapper wrapper, but then not passing it to the case. Working code:

    import inspect
    
    def labelable_yielded_case(case):
    
        argspec = inspect.getargspec(case)
        if argspec.defaults is not None:
            defaults_list = [''] * (len(argspec.args) - len(argspec.defaults)) + argspec.defaults
        else:
            defaults_list = [''] * len(argspec.args)
        argument_defaults_list = zip(argspec.args, defaults_list)
        case_wrappers = []
    
        def add_description(wrapper_id, argument_dict):
    
            case_wrappers[wrapper_id].description = case.__doc__.format(**argument_dict)
    
        def case_factory(*factory_args, **factory_kwargs):
    
            def case_wrapper_wrapper():
    
                wrapper_id = len(case_wrappers)
    
                def case_wrapper(*args, **kwargs):
    
                    argument_list = []
                    for argument in argument_defaults_list:
                        argument_list.append(list(argument))
                    for index, value in enumerate(args):
                        argument_list[index][1] = value
                    argument_dict = dict(argument_list)
                    argument_dict.update(kwargs)
                    add_description(wrapper_id, argument_dict)
                    return case(**argument_dict)
    
                case_wrappers.append(case_wrapper)
                case_name = case.__name__ + str(wrapper_id)
                case_wrapper.__name__ = case_name
                if factory_args:
                    setattr(factory_args[0].__class__, case_name, case_wrapper)
                    return getattr(factory_args[0].__class__, case_name)
                else:
                    return case_wrapper
    
            return case_wrapper_wrapper()
    
        return case_factory
    
    
    class TestTest(object):
    
        def __init__(self):
    
            self.data = None
    
        def setUp(self):
    
            self.data = (1,2,3)
    
        def test_all(self):
    
            for index, value in enumerate((1,2,3)):
                yield self.validate_equality(), index, value
    
        @labelable_yielded_case
        def validate_equality(self, index, value):
            '''element {index} equals {value}'''
    
            assert self.data[index] == value, 'expected %d got %d' % (value, self.data[index])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have a view passing on information from a database: def serve_article(request, id): served_article
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.