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 894721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:26:29+00:00 2026-05-15T14:26:29+00:00

I have a Python class and its methods are partially covered by unit tests

  • 0

I have a Python class and its methods are partially covered by unit tests like this:

class MyClass:

    def __init__(self):

       self.instancevar1 = None # some methods change this value
       self.instancevar2 = None # some methods change this value
       ...

    def method1(self, input_data):

        ...
        <self.instancevar1 and self.instancevar2 are used here>

        <return True or False>

...

class TestMyClass(unittest.TestCase):

    def test_method1(self):

        mc = MyClass()
        input_data = 'foo'
        self.assertEqual(mc.method1(input_data), True)

The tests are passing for simple cases like this.

I also have other classes that use instances of MyClass like this:

class MyOtherClass:

    def __init__(self):

        self.mc = MyClass()

Now, in some method of MyOtherClass, I have a situation like this:

class MyOtherClass:

    ...

    def some_method(self):

        ...
        result = self.mc.method1('foo') # result is expected to be True, but it is False

I think I understand the reason why this might happen. In the test case, I instantiate MyClass and call the test. In MyOtherClass, self.mc is referenced multiple times, misc methods of it are called and they probably change the internal state of the object (some values of instance variables), thus producing a different return value.

What are the suggested best practices for debugging such issues?

The fact that I have some tests for MyClass is good, but doesn’t seem to help much here. Does this example contain some design flaws which cause my problems?

  • 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-15T14:26:29+00:00Added an answer on May 15, 2026 at 2:26 pm

    As Rudi said, there’s either a bug in MyClass.method1() that causes it to return False when it shouldn’t, or there’s a bug in other parts of the code that puts mc in a state you didn’t expect.

    You can address both problems with additional unit tests:

    • If there’s a bug in MyClass.method1() when mc is in a particular state, you should add more unit tests to verify that mc.method1() returns the correct value for each state that mc is in.

    • Also, add unit tests for MyOtherClass and other relevant parts of the code to ensure that self.mc is always in the state you’re expecting, regardless of what operations you perform on the MyClass instance.

    It’s hard to come up with more specific advice than this since your example is fairly abstract, but I think you should get an idea about where to start looking and how to improve your tests to handle these issues better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python class I want to instantiate and the __init__ definition has
I have written a class in python that implements __str__(self) but when I use
For example, suppose I have the code: class Foo(object): def bar(self, x): '''blah blah
I have a python class which reads a config file using ConfigParser: Config file:
You have a Python class which needs an equals test. Python should use duck-typing
Suppose I have a Python class that I want to add an extra property
I have a model called Customer in google app engine with python: class Customer(db.Model):
I have a Python file with as content: import re import urllib class A(object):
Possible Duplicate: Circular (or cyclic) imports in Python I have class B that imports
As part of the last assignment in a beginner python programing class, I have

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.