1# -*- coding: utf-8 -*-
2#
3# gPodder - A media aggregator and podcast client
4# Copyright (c) 2005-2018 The gPodder Team
5#
6# gPodder is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 3 of the License, or
9# (at your option) any later version.
10#
11# gPodder is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18#
19
20
21# Run Doctests and Unittests for gPodder modules
22# 2009-02-25 Thomas Perl <thp@gpodder.org>
23
24
25import doctest
26import sys
27import unittest
28
29try:
30    # Unused here locally, but we import it to be able to give an early
31    # warning about this missing dependency in order to avoid bogus errors.
32    import minimock
33except ImportError as e:
34    print("""
35    Error: Unit tests require the "minimock" module (python-minimock).
36    Please install it before running the unit tests.
37    """, file=sys.stderr)
38    sys.exit(2)
39
40# Main package and test package (for modules in main package)
41package = 'gpodder'
42test_package = '.'.join((package, 'test'))
43
44suite = unittest.TestSuite()
45coverage_modules = []
46
47
48# Modules (in gpodder) for which doctests exist
49# ex: Doctests embedded in "gpodder.util", coverage reported for "gpodder.util"
50doctest_modules = ['util', 'jsonconfig']
51
52for module in doctest_modules:
53    doctest_mod = __import__('.'.join((package, module)), fromlist=[module])
54
55    suite.addTest(doctest.DocTestSuite(doctest_mod))
56    coverage_modules.append(doctest_mod)
57
58
59# Modules (in gpodder) for which unit tests (in gpodder.test) exist
60# ex: Tests are in "gpodder.test.model", coverage reported for "gpodder.model"
61test_modules = ['model']
62
63for module in test_modules:
64    test_mod = __import__('.'.join((test_package, module)), fromlist=[module])
65    coverage_mod = __import__('.'.join((package, module)), fromlist=[module])
66
67    suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(test_mod))
68    coverage_modules.append(coverage_mod)
69
70try:
71    # If you want a HTML-based test report, install HTMLTestRunner from:
72    # http://tungwaiyip.info/software/HTMLTestRunner.html
73    import HTMLTestRunner
74    REPORT_FILENAME = 'test_report.html'
75    runner = HTMLTestRunner.HTMLTestRunner(stream=open(REPORT_FILENAME, 'w'))
76    print("""
77    HTML Test Report will be written to %s
78    """ % REPORT_FILENAME)
79except ImportError:
80    runner = unittest.TextTestRunner(verbosity=2)
81
82try:
83    import coverage
84except ImportError:
85    coverage = None
86
87if __name__ == '__main__':
88    if coverage is not None:
89        cov = coverage.Coverage()
90        cov.erase()
91        cov.start()
92
93    result = runner.run(suite)
94
95    if not result.wasSuccessful():
96        sys.exit(1)
97
98    if coverage is not None:
99        cov.stop()
100        cov.report(coverage_modules)
101        cov.erase()
102    else:
103        print("""
104        No coverage reporting done (Python module "coverage" is missing)
105        Please install the python-coverage package to get coverage reporting.
106        """, file=sys.stderr)
107