0

I am trying to write unittests in python 2.3. I've got everything working accept the class level setUp and tearDown function. I've tried to look online but am not able to find a way to define these in a python 2.3 unittest case.

Hence, is it possible to have setUpClass() & tearDownClass() in a python 2.3 testcase? If yes, how do I go about doing that?

Danish
  • 3,708
  • 5
  • 29
  • 48
  • 4
    Historical note: Python 2.3 was released July 29, 2003. – miku Aug 24 '11 at 16:40
  • What have you tried so far? I don't know if it's different in 2.3 but you simply define the setUp function in your unittest.TestCase-inherited class. – Manny D Aug 24 '11 at 16:40
  • @miku I realize that, I have to use python 2.3 due to compatibility with a legacy system. If it were up to me, I'd be on 2.7 – Danish Aug 24 '11 at 16:44
  • @Manny D - the 'setUp()' function is run before *each* test. I want to add setup and cleanup for the whole test suite i.e. setup before *all* test cases and cleanup after *all* test cases – Danish Aug 24 '11 at 16:46
  • @Danish - No offence, I thought of it as a given. – miku Aug 24 '11 at 16:46
  • @Danish Yeah, I'm aware of how it works (I'm actually writing some now). I just wasn't sure if it was something they added in a later version or something. – Manny D Aug 24 '11 at 16:52
  • 1
    It may be a tough pill to swollow, but each test case should stand alone, and not depend on running after some multi-test setup/teardown. Common setup is a good reason to group tests into a suite, but the setup really should be occuring before *every* test case; doing otherwise makes it harder to isolate failures in individual test cases. – SingleNegationElimination Aug 24 '11 at 19:10

2 Answers2

0

According to the Python 2.3 standard library documentation, unittest.TestCase subclassing should work in the same way it works in more recent versions of the language.

import unittest

class MyTest(unittest.TestCase):

    def setUp(self):
        'Do stuff to set up before each test'

    def tearDown(self):
        'Do stuff to tear down after each test'

If there was something more specific that you're trying to accomplish, could you describe it?

BenTrofatter
  • 2,148
  • 1
  • 13
  • 13
  • I'm looking to implement setUpClass() and tearDownClass() that can be defined with @classmethod 2.7 as outlined here http://docs.python.org/library/unittest.html#unittest.TestCase – Danish Aug 24 '11 at 16:51
  • `@classmethod` is not available in 2.3 and was not able to find how to do `setUpClass()` and `tearDownClass()` in 2.3's documentation – Danish Aug 24 '11 at 16:53
  • I was just reading through the source for 2.3's unittest, and it looks like if you want to be able to take advantage of setUpClass and tearDownClass functionality, you'll have to write your own test loader or runner that implements those hooks. That being said, the unittest library has grown considerably since 2.3, and the trouble it will take might not have the best ROI. – BenTrofatter Aug 24 '11 at 17:12
  • 1
    Thanks for confirming the impression that I got as well. For the time being, I'm using the alphabetical ordering of the tests to have two special test e.g. `test_00()` and `test_99()` with all the actual tests as `test_XX_description()`. This was I can simulate what I want. Once we upgrade, I can switch these to the `@classmethod` versions – Danish Aug 24 '11 at 17:32
0

Try to use nose or other runner that is compatible with python 2.3; The python 2.3 runner didn't know about this methods (setUpClass/tearDownClass).

In python 2.3 to decorate a function you should do manually (no syntactic sugar) like:

class MyTest(unittest.TestCase):
    def setUpClass(cls): ...
    setUpClass = classmethod(setUpClass)
Nicolae Dascalu
  • 3,425
  • 2
  • 19
  • 17