473,412 Members | 2,087 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,412 software developers and data experts.

dynamic unittest

Hello,

I decided to re-use functionality of "unittest" module for my purposes.
More precisely, I have a list of folders. For each folder, code should
enter to the folder, execute a command and assert the output. It's
reasonable to use "unittest" here, but the problem is that "unittest"
doesn't support (== I haven't found how) dynamic creation of tests.

I thought it would be very easy, but due to lack of closures in Python
(more precisely, closures do exist, but they are counter-intuitive for
lisp programmers), I failed. Finally, I found a way using a global
variable. I exploit the fact that "unittest" uses "cmp" to arrange test
cases. Therefore, if we have an array of test names and sequential
number of running the common function, we know the name of the current
test. Here is a sample code:

------- <dynamic_unittest.py>

import sys, unittest
test_names = ['aaa', 'ddd', 'bbb']
test_names.sort()
test_index = 0

class DynamicTestCase(unittest.TestCase):
def one_test(self):
global test_index
test_name = test_names[test_index]
test_index = test_index + 1
# Ok for 'aaa' and 'bbb', failure for 'ddd'
assert test_name in ['aaa', 'bbb']

for test_name in test_names:
func_name = 'test_' + test_name
setattr(DynamicTestCase, func_name, DynamicTestCase.one_test)

suite = unittest.makeSuite(DynamicTestCase, 'test_')
if not unittest.TextTestRunner().run(suite).wasSuccessful ():
sys.exit(1)

------- </dynamic_unittest.py>

I think this code might help others, so I post it to the group.
Comments are welcome.

--
Oleg Parashchenko olpa@ http://xmlhack.ru/ XML news in Russian
http://uucode.com/blog/ Generative Programming, XML, TeX, Scheme
XSieve at XTech 2006: http://xtech06.usefulinc.com/schedule/detail/44

Jul 17 '06 #1
2 2427
Oleg Paraschenko wrote:
Hello,

I decided to re-use functionality of "unittest" module for my purposes.
More precisely, I have a list of folders. For each folder, code should
enter to the folder, execute a command and assert the output. It's
reasonable to use "unittest" here, but the problem is that "unittest"
doesn't support (== I haven't found how) dynamic creation of tests.

I thought it would be very easy, but due to lack of closures in Python
(more precisely, closures do exist, but they are counter-intuitive for
lisp programmers), I failed. Finally, I found a way using a global
variable. I exploit the fact that "unittest" uses "cmp" to arrange test
cases. Therefore, if we have an array of test names and sequential
number of running the common function, we know the name of the current
test. Here is a sample code:

------- <dynamic_unittest.py>

import sys, unittest
test_names = ['aaa', 'ddd', 'bbb']
test_names.sort()
test_index = 0

class DynamicTestCase(unittest.TestCase):
def one_test(self):
global test_index
test_name = test_names[test_index]
test_index = test_index + 1
# Ok for 'aaa' and 'bbb', failure for 'ddd'
assert test_name in ['aaa', 'bbb']

for test_name in test_names:
func_name = 'test_' + test_name
setattr(DynamicTestCase, func_name, DynamicTestCase.one_test)

suite = unittest.makeSuite(DynamicTestCase, 'test_')
if not unittest.TextTestRunner().run(suite).wasSuccessful ():
sys.exit(1)

------- </dynamic_unittest.py>

I think this code might help others, so I post it to the group.
Comments are welcome.
Here's what your code might look like with closures:

import sys
import unittest

test_names = ['aaa', 'ddd', 'bbb']
test_names.sort()

class DynamicTestCase(unittest.TestCase):
pass

def make_test(test_name):
def one_test(self):
self.assert_(test_name in ['aaa', 'bbb'])
return one_test

for test_name in test_names:
setattr(DynamicTestCase, 'test_' + test_name, make_test(test_name))

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

Peter
Jul 17 '06 #2
Hello Peter,

thanks a lot. I've overlooked this simple way to create the right
closure.

--
Oleg

Jul 17 '06 #3

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...
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...
1
by: Thomas Heller | last post by:
I'm trying to integrate some doctest tests with unittest. The tests must be exposed as one or more subclasses of unittest.TestCase, so I'm collecting them with a call to doctest.DocTestSuite(),...
41
by: Roy Smith | last post by:
I've used the standard unittest (pyunit) module on a few projects in the past and have always thought it basicly worked fine but was just a little too complicated for what it did. I'm starting a...
7
by: Jorgen Grahn | last post by:
I have a set of tests in different modules: test_foo.py, test_bar.py and so on. All of these use the simplest possible internal layout: a number of classes containing test*() methods, and the good...
5
by: Sakcee | last post by:
Hi I am trying to use pyUnit to create a framework for testing functions I am reading function name, expected output, from a text file. and in python generating dynamic test functions...
3
by: David Vincent | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hello I'm hoping to get some insight into a situation that seems odd to me. My Python experience is limited; I've just started using the unittest module....
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...
1
by: Chris Fonnesbeck | last post by:
I have a module for which I am trying to code a unit test. However, when I run unittest.main(), I get: In : import PyMC In : PyMC.unittest.main() ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.