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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:36:49+00:00 2026-06-15T02:36:49+00:00

I want to use Python 3.3 with unit tests in small self-contained program, i.e.

  • 0

I want to use Python 3.3 with unit tests in small self-contained program, i.e. I don’t want to split it up into a command line part and a “functional” part, which can be tested if it is started on itself on the command line.

So I have this little program:

import unittest

def stradd(a, b):
   return a + b

class test_hello(unittest.TestCase):
   def test_1(self):
      self.assertEqual(stradd("a", "b"), "ab")

unittest.main()
print(stradd("Hello, ", "world"))

Unfortunately, the print() is never reached, since unittest.main() exits the program. And even if it would not exit, it would print all kinds of output to the screen that I don’t want to see in normal operation.

Is there a way to run the tests silently, as long as there is no error? Of course, they should complain loudly if something doesn’t work.

I’ve seen Run python unit tests as an option of the program, but that doesn’t answer my question as well.

  • 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-15T02:36:50+00:00Added an answer on June 15, 2026 at 2:36 am

    It is possible to achieve the effect you want with a plain unittest module. You just need to write your own simple test runner. Like this:

    import unittest
    
    def stradd(a, b):
        return a + b
    
    class test_hello(unittest.TestCase):
        def test_1(self):
            self.assertEqual(stradd("a", "b"), "ab")
    
    
    def run_my_tests(test_case):
        case = unittest.TestLoader().loadTestsFromTestCase(test_case)
        result = unittest.TestResult()
        case(result)
        if result.wasSuccessful():
            return True
        else:
            print("Some tests failed!")
            for test, err in result.failures + result.errors:
                print(test)
                print(err)
            return False
    
    
    if run_my_tests(test_hello):
        # All tests passed, so we can run our programm.
        print(stradd("Hello, ", "world"))
    

    run_my_tests function will return True if all tests pass successfully. But if there is a test failure, it will print all errors/failures to stdout. For example:

    $ python myscript.py 
    Hello, world
    
    $ # And now the test fails...
    $ python myscript.py 
    Some tests failed!
    test_1 (__main__.test_hello)
    Traceback (most recent call last):
      File "myscript.py", line 8, in test_1
        self.assertEqual(stradd("a", "c"), "ab")
    AssertionError: 'ac' != 'ab'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use python re.split() to split a string into individual words by
I want to use the google maps hand cursor in Python but I don't
$whois abc.com I want to use python to hit this command, and then give
I have a multi-threaded program. I want to embed a python interpreter. I don't
I am trying to use python's unittest library to write some unit tests. I
I try to use mock to write some unit-tests in python. For example I
I want to use Python to run the cp command on Linux, thus copying
I want to use Python to access MFC document files generically? Can CArchive be
I want to use some Python libraries to replace MATLAB. How could I import
I use python -m SimpleHTTPServer when I want to run my web application locally,

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.