473,383 Members | 1,805 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,383 software developers and data experts.

pyunit: remove a test case on the fly

We have a number of TestCase classes that have multiple test methods.
We are interested in removing any of the individual test methods on the
fly (dynamically, at runtime, whatever).

We currently have an "isSupported" method in the TestCase classes that
return a bool by which the greater test harness decides whether to run
the enclosed test methods or not. We would like to have
per-test-method granularity, however, for essentially skipping that
particular test method; basically another isSupported call within the
individual methods. We took a quick look at such options as:
generating the entire test suite, then iterating through and renaming
SomeTestClass.testSomeTest to SomeTestClass.SKIPsomeTest, and seeing if
PyUnit would skip it (since the method name no longer starts with
"test"). But this seemed pretty unwieldy. We have also considered
breaking up the test class to a number of smaller test classes and
calling isSupported with that finer-granularity set of test classes.
That is probably the easiest way, but we do still want to consider
alternatives.

Here's to hoping that other folks out there have had experience with
removing test methods on the fly, and have some wisdom to share.

Cheers

Jul 19 '05 #1
2 2097
On 15 Jun 2005 14:13:09 -0700, chris <ch*********@gmail.com> wrote:
We have a number of TestCase classes that have multiple test methods.
We are interested in removing any of the individual test methods on the
fly (dynamically, at runtime, whatever).


Here's a simple approach imitating NUnit's CategoryAttribute. I don't
know whether it'll work for you, it depends on what exactly
isSupported() does.

- kv
import unittest

def category(*test_categories):
tc = frozenset(test_categories)
def f(g):
if tc & frozenset(active_categories):
return g
# else return None,
# effectively removing test from TestCase
return f

active_categories = ['mac', 'nt']

class T(unittest.TestCase):

@category('mac')
def test_a(self):
print 'mac only'

@category('posix')
def test_b(self):
print 'posix only'

@category('posix', 'nt')
def test_c(self):
print 'posix or nt'

def test_d(self):
print 'platform-independent'
if __name__ == '__main__':
unittest.main()
Jul 19 '05 #2
chris wrote:
We have a number of TestCase classes that have multiple test methods.
We are interested in removing any of the individual test methods on the
fly (dynamically, at runtime, whatever).

We currently have an "isSupported" method in the TestCase classes that
return a bool by which the greater test harness decides whether to run
the enclosed test methods or not. We would like to have
per-test-method granularity, however, for essentially skipping that
particular test method; basically another isSupported call within the
individual methods. We took a quick look at such options as:
generating the entire test suite, then iterating through and renaming
SomeTestClass.testSomeTest to SomeTestClass.SKIPsomeTest, and seeing if
PyUnit would skip it (since the method name no longer starts with
"test"). But this seemed pretty unwieldy. We have also considered
breaking up the test class to a number of smaller test classes and
calling isSupported with that finer-granularity set of test classes.
That is probably the easiest way, but we do still want to consider
alternatives.


ISTM you could write a custom TestLoader that filters the test names, then pass that loader to unittest.main(). For example, assuming a classmethod or staticmethod on the test case that filters test case names, something like this (not tested):

def MyTest(TestCase):
@staticmethod
def isSupportedTest(testName):
return True

def testSomething...

class FilteringTestLoader(TestLoader):
def getTestCaseNames(self, testCaseClass):
names = TestLoader.getTestCaseNames(self, testCaseClass)
names = filter(testCaseClass.isSupportedTest, names)
return names

unittest.main(testLoader=FilteringTestLoader())

You could do something similar with a FilteringTestSuite if that fits your harness better.

Kent
Jul 19 '05 #3

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

Similar topics

2
by: could ildg | last post by:
I want to know something about unittest these days, and since I'm learning python, I want to touch it through python. But when I found the newest pyunit is even so old, I wonder if it is still...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
1
by: Steve Jorgensen | last post by:
I was working with a friend on a project Monday night, and tried to run a pyunit test from Eclipse, and nothing seemed to happen. We finally figured out that the test is doing exactly what it's...
1
by: travislspencer | last post by:
Hey All, I am trying to write a script that runs all of my pyunit tests for me. Ideally, I would like to be able to drop a new module into my project's test subdirectory, and the testing script...
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...
1
by: Noah | last post by:
Is there a way for a test case method in a class derived from unittest.TestCase to tell if the harness was started with the verbose option? I would like my test cases to print out a little extra...
2
by: volcano | last post by:
I am desperately looking for an info how to combine a testing application with decent GUI interface - the way most xUnits do. I believe I have seen something about using Tkinter, but I do not...
0
by: winston.yang | last post by:
Is it possible to use PyUnit to test with multiple threads? I want to send many commands to a database at the same time. The order of execution of the commands is indeterminate, and therefore, so...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.