root / guide / doctest.py

Revision 144:60ec3971f3f6, 1.7 kB (checked in by Tarek Ziad?? <tarek@…>, 11 months ago)

Added scripts from the book

Line 
1#!/usr/bin/python
2# -*- coding: UTF-8 -*-
3#
4# Copyright (c) 2007 Tarek Ziadé
5#
6# Authors:
7#   Tarek Ziadé <tarek@ziade.org>
8#
9# This program is free software; you can redistribute it and/or
10# modify it under the terms of the GNU General Public License
11# as published by the Free Software Foundation; either version 2
12# of the License, or (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22import doctest
23import unittest
24import os
25import sys
26
27def doc_suite(test_dir, setUp=None, tearDown=None, globs=None):
28    """returns a test suite, based on docs"""
29    suite = []
30    if globs is None:
31        globs = globals()
32
33    flags = (doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE |
34             doctest.REPORT_ONLY_FIRST_FAILURE)
35
36    product_dir = os.path.split(test_dir)[0]
37    if product_dir not in sys.path:
38        sys.path.append(product_dir)
39
40    doc_dir = os.path.join(product_dir, 'doc')
41    docs = [os.path.join(doc_dir, doc) for doc in
42            os.listdir(doc_dir) if doc.endswith('.txt')]
43
44    for test in docs:
45        suite.append(doctest.DocFileTest(test,
46                     optionflags=flags, globs=globs,
47                     setUp=setUp, tearDown=tearDown,
48                     module_relative=False))
49
50    return unittest.TestSuite(suite)
Note: See TracBrowser for help on using the browser.