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

  • SEARCH
  • Home
  • 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 5969937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:18:08+00:00 2026-05-22T20:18:08+00:00

I’m using python-inject , python 2.6 (with unittest2). We have classes to test that

  • 0

I’m using python-inject, python 2.6 (with unittest2).

We have classes to test that use injection, and test-cases which also use the same values. We currently use inject.appscope, to “singletonize” the values. Otherwise they’re instantiated per user.

import inject

class A(object):
    '''shared data between many parts'''
    ...

class Foo(object):
    '''uses a to do stuff'''

    a = inject.attr('a', A)

    def bar(self):
        self.a.doStuff()

class TestFoo(TestCase):
    a = inject.attr('a', A)

    def setUp(self):
        self.foo = Foo()

    def testBar(self):
        self.foo.bar()
        self.assertTrue(self.a.stuffWasDone())
        self.assertFalse(self.a.stuffWasDoneTwice())

    def testBarTwice(self):
        self.foo.bar()
        self.foo.bar()
        self.assertTrue(self.a.stuffWasDoneTwice())


injector = inject.Injector()
injector.bind(A, scope=inject.appscope)
inject.register(injector)

runTests()

Ideally, I’d like to reset A after each test run, to avoid cross-contamination.
(Currently do something like A.reset() in tearDown()..)

inject.reqscope supports something like this (a locally scoped set of instances), but I don’t really know where to call register() & unregister() (which resets the injection objet cache). Ot’s too late in setUp() and tearDown(), since Foo.a may already be called in Foo.__init__() for each of them.

Any ideas on how to do this, or should I use a different approach?

  • 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-05-22T20:18:09+00:00Added an answer on May 22, 2026 at 8:18 pm

    You can use setUp and tearDown to register/unregister the scope, treating each test method invocation as a new “request”.

    With the following changes your sample unit tests pass:

    class TestFoo(unittest2.TestCase):
    
        a = inject.attr('a', A)
    
        def __init__(self, *n, **kw):
            unittest2.TestCase.__init__(self, *n, **kw)
            self.unit_scope = inject.reqscope
    
        def setUp(self):
            self.foo = Foo()
            self.unit_scope.register()
    
        def tearDown(self):
            self.unit_scope.unregister()
    
        ...
    

    And bind your A instance using inject.reqscope:

    injector = inject.Injector()
    injector.bind(A, scope=inject.reqscope)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.