473,233 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,233 software developers and data experts.

unittest setup

hi all,

I noticed that setUp() and tearDown() is run before and after *earch*
test* method in my TestCase subclasses. I'd like to run them *once* for
each TestCase subclass. How do I do that.

thanks
paul

Sep 25 '05 #1
5 3534
paul kölle wrote:
hi all,

I noticed that setUp() and tearDown() is run before and after *earch*
test* method in my TestCase subclasses. I'd like to run them *once* for
each TestCase subclass. How do I do that.


Create a global/test instance flag.

Diez
Sep 25 '05 #2
Diez B. Roggisch wrote:
paul kölle wrote:
hi all,

I noticed that setUp() and tearDown() is run before and after *earch*
test* method in my TestCase subclasses. I'd like to run them *once* for
each TestCase subclass. How do I do that.

Create a global/test instance flag.


I'm not sure if I understood what you mean, I tried:

setup = 'down'

class BaseTest(unittest.TestCase):
def setUp(self):
global setup
if setup == 'up':
print 'Not running setUp() again...'
return
...
all setup work goes here.
...
setup = 'up'
This didn't work, (tried to reset the flag in the last test* method to
'down', no dice)
and:

class BaseTest(unittest.TestCase):
def __init__(self, ...):
unittest.TestCase.__init__(self, ...)
self.setup = 'down'

def setUp(self):
if self.setup == 'up':
return
dowork
self.setup = 'up'

Failed also, I'm not sure why, __init__ was called way too often and
self.setup was always reset to 'down'. I finally gave up and created my
own method which I call in *every* test* method which is ugly, leads to
longer runtime and code duplication.
But at least it encouraged me to read the unittest docs more carefully.
Now I seem to understand that:

TestSuite.addTest(TestCaseSubclass('testSomething' ))
TestSuite.addTest(TestCaseSubclass('testSomethingO ther'))

will create two instances of TestCaseSubclass, so there is no way that
'testSomethingOther' will ever see what 'testSomething' might have
created if all work is done with instance data right? Initially I
thought it goes like: "run setUp(), run all test* methods, run
tearDown()" and that is what the unittest docs call a "fixture"

<cite python 2.3 docs for unittest>
A test fixture represents the preparation needed to perform one or more
tests, and any associate cleanup actions.
</cite>

but further down:
<cite python 2.3 docs for unittest>
Each instance of the TestCase will only be used to run a single test
method, so a new fixture is created for each test.
</cite>

It seems to me my case is not that exotic, I thought it would be quite
natural to write the boilerplate stuff in setUp() and build on that to
step through the applications state with test* methods each building on
top of each other. Is that the wrong approach? Are there other
frameworks supporting such a style?

thanks
Paul

Sep 25 '05 #3
"paul kölle" <pa**@subsignal.org> wrote:

[snipped]

It seems to me my case is not that exotic, I thought it would be quite
natural to write the boilerplate stuff in setUp() and build on that to
step through the applications state with test* methods each building on
top of each other. Is that the wrong approach? Are there other
frameworks supporting such a style?


Yes, py.test: http://codespeak.net/py/current/doc/test.html.

George
Sep 26 '05 #4
[George Sakkis]
Yes, py.test: http://codespeak.net/py/current/doc/test.html.


The whole http://codespeak.net site contains many interesting projects,
which are all worth a good look!

However, there is a generic ``LICENSE`` file claiming copyrights on all
files, without explaining what the copyright conditions are. This file
also delegates copyright issues to individual files, which are usually
silent on the matter. Could this whole issue be clarified? Or did I
miss something I should not have?

--
François Pinard http://pinard.progiciels-bpi.ca
Sep 26 '05 #5
paul kölle wrote:
hi all,

I noticed that setUp() and tearDown() is run before and after *earch*
test* method in my TestCase subclasses. I'd like to run them *once* for
each TestCase subclass. How do I do that.


One way to do this is to make a TestSuite subclass that includes your startup and shutdown code.

For example I have some tests that rely on a webserver being started. I have a TestSuite that starts the server, runs the tests and stops the server. This way the server is only started once per test module. Here is the TestSuite class:

class CbServerTestSuite(unittest.TestSuite):
''' A test suite that starts an instance of CbServer for the suite '''
def __init__(self, testCaseClass):
unittest.TestSuite.__init__(self)
self.addTest(unittest.defaultTestLoader.loadTestsF romTestCase(testCaseClass))

def __call__(self, result):
CbServer.start()
unittest.TestSuite.__call__(self, result)
CbServer.stop()
I use it like this:

class MyTest(unittest.TestCase):
def testWhatever(self):
pass

def suite():
return CbServerTestSuite(MyTest)

if __name__=='__main__':
unittest.TextTestRunner().run(suite())
This runs under Jython (Python 2.1); in more recent Python I think you can override TestSuite.run() instead of __call__().

Kent
Sep 30 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Will Stuyvesant | last post by:
I have a unittest testfile like this: ----------------------- test_mod.py --------------------- import sys sys.path.append('..') import unittest import mod class...
2
by: JAWS | last post by:
I get this error message when trying to run a unittest test with a dynamically created test method: Traceback (most recent call last): File "unittest.py", line 215, in __call__ testMethod()...
0
by: Remy Blank | last post by:
Ok, here we go. I added the possibility for tests using the unittest.py framework to be skipped. Basically, I added two methods to TestCase: TestCase.skip(msg): skips unconditionally...
3
by: Qiangning Hong | last post by:
I want to apply TDD (test driven development) on my project. I am working on a class like this (in plan): # file: myclass.py import _extmod class MyClass(object): def __init__(self):...
0
by: Chris Fonnesbeck | last post by:
I have built the following unit test, observing the examples laid out in the python docs: class testMCMC(unittest.TestCase): def setUp(self): # Create an instance of the sampler...
2
by: Collin Winter | last post by:
While working on a test suite for unittest these past few weeks, I've run across some behaviours that, while not obviously wrong, don't strike me as quite right, either. Submitted for your...
1
by: winston.yang | last post by:
I've read that unittest.main() can take an optional argv argument, and that if it is None, it will be assigned sys.argv. Is there a way to pass command line arguments through unittest.main() to...
0
by: Nikolaus Rath | last post by:
Hello, I have a number of conceptually separate tests that nevertheless need a common, complicated and expensive setup. Unfortunately, unittest runs the setUp method once for each defined...
1
by: Jean-Paul Calderone | last post by:
On Tue, 29 Jul 2008 16:35:55 +0200, Nikolaus Rath <nikolaus@rath.orgwrote: class TwoTests(unittest.TestCase): setUpResult = None def setUp(self): if self.setUpResult is None:...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.