4

I can't find a way to do_something() when the assertion in a test passes. For example:

    def test_one(self):
        self.assertEqual(1,1, "Did not match")

That test will print "Did not match" if the assertion would fail, but in this case it will not, so I'm trying to call a function or print something when the self.assertEqual() is successful, please any ideas ?

Thanks

0

2 Answers 2

4

If you want to print when something passes, there are a couple of options. However, please don't use Noeld's answer. Not that it is wrong, it's just that you don't want to clutter your test with a bunch of print messages when unittest provides better ways for doing this.

Verbosity

If you want to print the result of every single test_ function, set the verbosity of the test runner. You can do this in a couple of ways:

  1. From the command line, use the verbose option:

    python -m unittest discover -v

  2. Programmatically calling the unittest.main and passing it the verbosity argument

    if __name__ == "__main__":
        unittest.main(verbosity=2)
    

Running tests "manually"

  1. Programmatically building up your TestSuite and calling the TestRunner with the verbosity argument.

    suite = unittest.TestLoader().loadTestsFromModule(TestModuleName)
    results = unittest.TextTestRunner(verbosity=2).run(suite)
    
  2. Creating a subclass of the TestResult object, which contains an addSuccess method which will be called whenever a test passes.

    You can then pass this TestResult object to the Test Suite's run method.

        suite = unittest.TestLoader().loadTestsFromModule(TestModuleName)
        suite.run(myTestResult)
    

Third party runners

Take a look at Twisted's Trial. It contains many different Test Runners which might be useful. By default, it runs the TreeReporter which looks like:

Trial reporting

Sign up to request clarification or add additional context in comments.

Comments

1

This works:

import unittest


class TestupSomeStuff(unittest.TestCase):
    def setUp(self):
        pass

    def test_fail(self):
        x = 2 * 4
        self.assertEqual(x,9,"DID NOT MATCH")
        print "Passed"

    def test_pass(self):
        x = 2 * 4
        self.assertEqual(x,8,"DID NOT MATCH")
        print "Passed"





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

This is because if your assert fails then the test is a fail and you go to the next test, however when the test is a success it carries on and returns None!

For example:

import unittest


class TestupSomeStuff(unittest.TestCase):
    def setUp(self):
        pass

    def test_fail(self):
        return None
        # This is skipped, so test is win
        x = 2 * 4
        self.assertEqual(x,9,"DID NOT MATCH")
        print "FAIL"

    def test_pass(self):
        x = 2 * 4
        self.assertEqual(x,8,"DID NOT MATCH")
        # Passed gets printed after the dot for passing the previous test.
        print "Passed"

Prints:

.Passed
.
----------------------------------------------------------------------
Ran 2 tests in 0.000s

Hope this helps.

3 Comments

While this helps accomplish what the OP wants, there are many different ways of accomplishing the same thing using built-in tools.
Fair point just wanted to demo a simple way of printing a message with out having to subclass methods to get a different message. Nice answer.
using print is a bad idea when you use Unittest , it has a habit of mangling stdout and it can often make it unclear when a print statement was actually fired. A better approach is to not rely on stdout but to use pythons logging subsystem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.