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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:23:28+00:00 2026-06-04T17:23:28+00:00

I’m trying to test a class using Python unittest, but the test suite seems

  • 0

I’m trying to test a class using Python unittest, but the test suite seems to be modifying the base class

here’s some pseudo code to demonstrate the problem:

the class

// module cards...
class deck():

    def __init__(self, cards):
        self.__cards = cards

    def __len__(self):
        return len(self.__cards)

    def draw(self, n = 1):
        '''
        remove and return n cards
        from internal card list

        n -- integer
        @return list removed
        '''
        removed      = self.__cards[0:n]
        remaining    = self.__cards[n:]
        self.__cards = remaining
        return removed

    def addOne(self, card):
        '''
        add single card to the deck
        '''
        self.__cards.append(card)

    def addMany(self, cards):
        '''
        add many cards to the deck
        '''
        self.__cards.extend(cards)

a fixture to populate the instance, will probably be the output of a file or DB query

//source
src = [
{
    'id'          : 1,
    'name'        : 'one',
    'description' : 'this is one'
},
{
    'id'          : 2,
    'name'        : 'two',
    'description' : 'this is two'
},
{
    'id'          : 3,
    'name'        : 'three',
    'description' : 'this is three'
},
{
    'id'          : 4,
    'name'        : 'four',
    'description' : 'this is four'
},
{
    'id'          : 5,
    'name'        : 'five',
    'description' : 'this is five'
}
]

the tests

from source import src
import cards 

def test_drawRemovesOne(self):
    deck = cards.deck(src)
    self.assertTrue(callable(deck.draw))

    # single card
    deckSize  = len(deck)
    drawnCard = deck.draw(1)
    self.assertEqual(drawnCard[0]['id'], 1)
    self.assertEqual(4, len(deck))
    self.assertEqual(len(drawnCard), 1)



def test_drawRemovesMany(self):
    deck = cards.deck(src)
    deckSize   = len(deck)
    drawnCards = deck.draw(3)
    self.assertEqual(drawnCards[0]['id'], 1)
    self.assertEqual(drawnCards[1]['id'], 2)
    self.assertEqual(drawnCards[2]['id'], 3)
    self.assertEqual(len(deck), 2)
    self.assertEqual(len(drawnCards), 3)


'''
INVALIDATES PREVIOUS TESTS...
def test_addOne(self):
    deck = cards.deck(src)
    card = {
        'id'          : 9,
        'name'        : 'nine',
        'description' : 'this is nine'
    }
    deckSize = len(deck)
    deck.addOne(card)
    newDeckSize = len(deck)
    self.assertTrue(newDeckSize == deckSize + 1)
'''

EDIT: posted real snippet

the first test passes, but then subsequently fails when second test is implemented. It seems as if there is one reference to the class being shared by the test methods, despite appearing to be fresh instances.

I’ve only done a nominal bit of work in Python, is this the result of some language peculiarity I’m unaware of?

  • 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-04T17:23:30+00:00Added an answer on June 4, 2026 at 5:23 pm

    See this code

    >>> class Deck:
    ...     def __init__(self, cards):
    ...         self.cards = cards
    >>> l0 = [1, 2, 3]
    >>> d1 = Deck(l0)
    >>> d2 = Deck(l0)
    >>> d1.cards
    0: [1, 2, 3]
    >>> d2.cards
    1: [1, 2, 3]
    >>> d1.cards.append(4)
    >>> d2.cards
    2: [1, 2, 3, 4]    # d1 and d2 share the same list for cards
    

    You start both your tests with deck = cards.deck(src) but I don’t know where that src is coming from. If it isn’t created in your setUp method as a new object then it could be being shared between the test cases.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.