473,383 Members | 1,859 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.

Unittest - adding a doctest suite to unittest.main

My normal testing consists of a tests.py script using unittest, with
the basic

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

incantation to get things going.

But I now want to incorporate some other tests (specifically, a text
file containing doctests) and I find that there is no gradual process
for expanding on unittest.main. It seems that I need to jump straight
into the depths of test suites, test loaders, and the rest, and build
my main routine from those. I've looked at the source for
unittest.main, and it doesn't look particularly customisable.

Just before I start diving into the gory details, have I missed a
simple way of adding an additional doctest.DocFileSuite to
unittest.main?

Thanks,
Paul.
Oct 14 '08 #1
3 1955
Paul Moore <p.*******@gmail.comwrote:
Just before I start diving into the gory details, have I missed a
simple way of adding an additional doctest.DocFileSuite to
unittest.main?
Create a function named test_suite which creates a test suite containing
all your tests including the doctests. Pass that to main as the defaultTest
argument.

Google for "DocFileSuite unittest.main" and you should find plenty of
examples. e.g. http://mail.zope.org/pipermail/zope3-checkins/2008-
May/029732.html or
http://svn.nuxeo.org/trac/pub/browse...es/snowsprint-
eventprovider/src/calcore/tests/test_cal.py?rev=50936

--
Duncan Booth http://kupuguy.blogspot.com
Oct 14 '08 #2
On 14 Oct, 16:09, Duncan Booth <duncan.bo...@invalid.invalidwrote:
Create a function named test_suite which creates a test suite containing
all your tests including the doctests. Pass that to main as the defaultTest
argument.
Ah, thanks. I see now - a suite is itself a test. That makes sense.

But how do I name the default "run all the unit tests in this module"
suite, so I can add to it rather than replace it? It's available as
the TestProgram.test attribute, but TestProgram's nasty behaviour of
running the test suite as part of its __init__ means I don't get a
chance to grab it before the tests run :-( (That's what I meant when I
said the default isn't very customisable).

Nearest I can gather, it's
unittest.defaultTestLoader.loadTestsFromNames(self .testNames,self.module)
but I can't get at those 2 attributes easily either! Pretty soon, I'll
have cut & pasted the whole class and modified it as I need.

The best approach I could find was to subclass unittest.TestProgram to
intercept the bit where it runs the tests in __init__:

class TestMain(unittest.TestProgram):
def runTests(self):
pass
def parentRunTests(self):
unittest.TestProgram.runTests(self)

Then I create the class, fiddle with the testmain.test attribute, and
then call testmain.parentRunTests(), but that feels like a really ugly
hack :-(
Google for "DocFileSuite unittest.main" and you should find plenty of
examples. e.g.http://mail.zope.org/pipermail/zope3-checkins/2008-
May/029732.html orhttp://svn.nuxeo.org/trac/pub/browser/Python/CalCore/branches/snowspr...
eventprovider/src/calcore/tests/test_cal.py?rev=50936
Thanks. I really should have done that (I searched the documentation
but not Google). Ironically, that query now pops up this thread as the
number one hit. Not sure what to make of that...

Paul.
Oct 14 '08 #3
Duncan Booth <du**********@invalid.invalidwrites:
Create a function named test_suite which creates a test suite
containing all your tests including the doctests. Pass that to main
as the defaultTest argument.
Better to name it ‘suite’, so that its name doesn't match the default
search for individual test functions.

--
\ “Remember: every member of your ‘target audience’ also owns a |
`\ broadcasting station. These ‘targets’ can shoot back.” —Michael |
_o__) Rathbun to advertisers, news.admin.net-abuse.email |
Ben Finney
Oct 14 '08 #4

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: Danny Shevitz | last post by:
Why doesn't the following code snippet work? The error is ImportError: No module named myTestCase2 TIA, Danny %<--------------------------------------------------------------------
3
by: Jan Decaluwe | last post by:
I'm working on a unit test for a finite state machine (FSM). The FSM behavior is specified in a dictionary called transitionTable. It has a key per state with a tuple of possible transitions as...
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...
24
by: john_sips_tea | last post by:
For writing testcode, it looks like there's three ways that it's typically done: (1). using the doctest module, (2). using the unittest module (i.e. "pyunit"), or else (3). just putting an...
2
by: Oleg Paraschenko | last post by:
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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...

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.