Search code examples
unit-testingnose

How can I see name of failed tests if setUpClass is failed?


please help me. How to see name of failed tests if setUpClass is failed?

EXAMPLE:

import unittest
import nose


class Test(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print "Execute setup class!"
        assert 10 != 10

    def test_1(self):
        """Test # 1"""
        pass

    def test_2(self):
        """Test # 2"""
        pass


if __name__ == "__main__":
    nose.run(argv=[" ", "work_temp.py", "--verbosity=2", "--nocapture"])

If some assertion in SetUp fails - I have bad output like this (can't see test name):

======================================================================
ERROR: test suite for <class 'tests.work_temp.Test'>
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/nose/suite.py", line 209, in run
    self.setUp()
.....................................................................
    return func()
  File "/home/temp/PycharmProjects/Safo/tests/work_temp.py", line 9, in setUpClass
    assert 10 != 10
AssertionError

----------------------------------------------------------------------
Ran 0 tests in 0.001s

FAILED (errors=1)

I expected to see something like this:

Test # 1 ... FAILED
Test # 2 ... FAILED

Solution

  • You are throwing an assert exception when you are creating the container class for your test methods, and the only logical behaviour in this case is not to run underlying tests. This is what nose does, but it does so to be compliant with python unittest specifications:

    If an exception is raised during a setUpClass then the tests in the class are not run and the tearDownClass is not run.

    If you are willing to step outside of unittest library, you can get the behaviour that you want by modifying your code something like this:

    from nose.tools import with_setup
    
    def setup_func():
        print "Execute setup class!"
        assert 10 != 10
    
    @with_setup(setup_func)
    def test_1():
        """Test # 1"""
        pass
    
    @with_setup(setup_func)
    def test_2():
        """Test # 2"""
        pass