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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:58:25+00:00 2026-05-15T10:58:25+00:00

I’m wondering what techniques people use for simplifying the ‘size’ of code used for

  • 0

I’m wondering what techniques people use for simplifying the ‘size’ of code used for unit testing. For example I was trying to marshal an object of the class and testing the marshal’ed object (but this presumes marshal is working correctly).

Consider the class

import unittest
class Nums(object):
    def __init__(self, n1_, n2_, n3_):
        self.n1, self.n2, self.n3 = n1_, n2_, n3_
def marshal(self):
    return "n1 %g, n2 %g, n3 %g"%(self.n1,self.n2,self.n3)

and then the marshaling based, list based, and normal tests

class NumsTests(unittest.TestCase):
    def setUp(self):
        self.nu = Nums(10,20,30)
    def test_init1(self):
        self.assertEquals(self.nu.marshal(),"n1 %g, n2 %g, n3 %g"%(10,20,30))
    def test_init2(self):
        self.assertEquals([self.nu.n1,self.nu.n2,self.nu.n3],[10,21,31])
    def test_init3(self):
        self.assertEquals(self.nu.n1,10)
        self.assertEquals(self.nu.n2,21)
        self.assertEquals(self.nu.n3,31)

which give the following errors (since, 20!=21 and 30!=31, my test has a bad initialization or the test conditions are wrong)

AssertionError: 'n1 10, n2 20, n3 30' != 'n1 10, n2 21, n3 31'

AssertionError: [10, 20, 30] != [10, 21, 31]

AssertionError: 20 != 21

The first and second error messages are difficult to understand (since you have to parse the string or list). However, the 3rd technique rapidly explodes in the amount of code used to test complex objects.

Is there a better way to simplify unit tests without creating difficult error messages? And, without depending on the veracity of a marshal function?

[changed test_marshal to marshal]

  • 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-15T10:58:26+00:00Added an answer on May 15, 2026 at 10:58 am

    For testing initialization, I recommend not testing via calling the marshal() function. Not only do you then have to parse out which initializer failed, you also have to figure out whether it’s your initialization that’s failing or the marshal function itself. The “minimal style” for unit tests I would recommend is to narrow down the focus of what you’re testing at any test point as much as is feasible.

    If I really had to test the initialization of a whole bunch of fields, I might refactor much the same way as Eli:

    class MyTestCase(unittest.TestCase):
        def assertFieldsEqual(self, obj, expectedFieldValDict):
            for fieldName, fieldVal in expectedFieldValDict.items():
                self.assertFieldEqual(obj, fieldName, fieldVal)
        def assertFieldEqual(self, obj, fieldName, expectedFieldVal):
            actualFieldVal = getattr(obj, fieldName)
            if expectedFieldVal != actualFieldVal:
                msg = "Field %r doesn't match: expected %r, actual %r" % \
                    (fieldName, expectedFieldVal, actualFieldVal)
                self.fail(msg)
    
    class NumsTests(MyTestCase):
        def setUp(self):
            self.initFields = {'n1': 10, 'n2': 20, 'n3': 30}
            self.nums = Nums(**initFields)
        def test_init(self):
            self.assertFieldsEqual(self.nums, self.initFields)
    

    “Good grief,” I can hear you say, “that’s a lot of code!” Well yeah, but the differences are:

    • assertFieldsEqual and assertFieldEqual are reusable functions that have been abstracted to a common test case class which your other test cases can reuse.
    • The failure message describes exactly what’s going on.

    When it comes time to test your marshal function, you can simply do this:

    def test_marshal(self):
        expected = '...'
        self.assertEqual(expected, self.nums.marshal())
    

    When comparing strings, though, I prefer a method that tells me exactly where the strings diverge. For multiline strings, there’s now a method for that in Python 2.7, but I’ve rolled or cribbed my own methods for this purpose in the past.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I have text I am displaying in SIlverlight that is coming from a CMS
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,

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.