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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:15:51+00:00 2026-06-07T14:15:51+00:00

unittest comes with many assert methods. I did a timeit test on using built-in

  • 0

unittest comes with many assert methods. I did a timeit test on using built-in Python assert and comparison operator vs built-in simple unittest assertion.

#!/usr/bin/python
import timeit

s = """\
import unittest
class TestRepomanManExtFunctions(unittest.TestCase):
    def test1(self):
        someObj = object()
        newObj = someObj
        self.assertEqual(someObj, newObj)

    def test2(self):
        str1 = '11111111111111111111111111111111111111'
        str2 = '33333333333333333333333333333333333333'
        self.assertNotEqual(str1, str2)


if __name__ == '__main__':
    unittest.main()

"""
t = timeit.Timer(stmt=s)
print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)

s2 = """\
import unittest
class TestRepomanManExtFunctions(unittest.TestCase):
    def test1(self):
        someObj = object()
        newObj = someObj
        assert someObj == newObj

    def test2(self):
        str1 = '11111111111111111111111111111111111111'
        str2 = '33333333333333333333333333333333333333'
        assert str1 != str2


if __name__ == '__main__':
    unittest.main()

"""

t = timeit.Timer(stmt=s2)
print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)

The results are

yeukhon@yeukhon-P5E-VM-DO:/tests$ python t.py
1203.46 usec/pass
873.06 usec/pass
yeukhon@yeukhon-P5E-VM-DO:tests$ vim t.py
yeukhon@yeukhon-P5E-VM-DO:tests$ python t.py
1300.33 usec/pass
956.35 usec/pass
yeukhon@yeukhon-P5E-VM-DO:tests$ python t.py
1208.82 usec/pass
865.18 usec/pass

One advantage of using the unittest’s built-in assert methods is that it tells the user what is actually being compared. An example from of my actual tests:

======================================================================
FAIL: test_000_write_to_file_directory_not_exist (__main__.TestRepomanManExtFunctions)
Test writing content to a new file whose parent directory
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/mock.py", line 1224, in patched
    return func(*args, **keywargs)
  File "test_ext.py", line 71, in test_000_write_to_file_directory_not_exist
    self.assertNotEqual(mk_exists.call_args_list, exists_call_list)
AssertionError: [call('/tmp/test/fake/')] == [call('/tmp/test/fake/')]

Here is using simple assert X = Y

======================================================================
FAIL: test_000_write_to_file_directory_not_exist (__main__.TestRepomanManExtFunctions)
Test writing content to a new file whose parent directory
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/mock.py", line 1224, in patched
    return func(*args, **keywargs)
  File "test_ext.py", line 72, in test_000_write_to_file_directory_not_exist
    assert exists_call_list != mk_exists.call_args_list
AssertionError

Besides this advantage, what other nice things can we do by utilising the built-in self.assert_*(...)?

Why is the raw faster? I know accessing attribute and checking against a class is generally slower. But I want to know what’s going on too? I hope this is a valid question.

Thanks

  • 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-07T14:15:52+00:00Added an answer on June 7, 2026 at 2:15 pm

    Probably the assert* function is slower because there is overhead in calling it (pushing parameters to stack, calling, popping return address from stack etc.), compared to the assert keyword which is executed inline. The assert* functions have some other nice properties as you have mentioned, like printing the expected and actual value.

    Are you asking out of curiosity or is this an actual problem? I’d be surprised to see any situation where the speed of asserts is a bottleneck.

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

Sidebar

Related Questions

How could you write a unittest in Python that would test that the output
my Ruby On Rails unit-test fails in a simple string comparison and I can't
I'm writing a unit test for some simple methods. The issue I am having
Using Visual Studio Test Suite, is there a way to make a single unit
I try to run this simplest unit test: test the truth do assert true
This answer explains how to create test cases dynamically. The answer's code: class Tests(unittest.TestCase):
Having recently gotten into test driven development I am using the Nunit test runner
I have the following code (as an example) in a unittest TestCase def test(self):
I have a unit test which contains the following line of code Site.objects.get(name=UnitTest).delete() and
I am using Moq, NUnit, WPF, MVVM, Ninject. I am writing a test for

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.