Add support for unit testing and doctesting

New module "gpodder.unittests" that will run all
unit tests and doctests (currently only gpodder.util)
for gPodder. The tests can easily be run with a

    make unittest

in the source tree or with the following command
when gPodder is installed onto the system:

    python -m gpodder.unittests

Also added a (currently unused) helper function to
gpodder.util that already has unittests prepared.
This commit is contained in:
Thomas Perl 2009-02-25 14:23:03 +01:00
parent dcefd98846
commit 043309cec4
3 changed files with 85 additions and 3 deletions

View file

@ -49,6 +49,7 @@ all: help
help:
@echo 'make test run gpodder in local directory'
@echo 'make unittest run doctests + unittests'
@echo 'make mtest run gpodder (for maemo scratchbox)'
@echo 'make release create source tarball in "dist/"'
@echo 'make releasetest run some tests before the release'
@ -69,6 +70,9 @@ test:
@echo -ne '\033]0;gPodder console (make test)\007'
$(BINFILE) --local --verbose
unittest:
PYTHONPATH=src/ python -m gpodder.unittests
mtest:
@# in maemo scratchbox, we need this for osso/hildon
run-standalone.sh $(BINFILE) --local --maemo --verbose
@ -143,7 +147,7 @@ distclean: clean
##########################################################################
.PHONY: all test release releasetest install update-icons generators gen_manpage gen_graphics clean distclean messages help
.PHONY: all test unittest release releasetest install update-icons generators gen_manpage gen_graphics clean distclean messages help
##########################################################################

44
src/gpodder/unittests.py Normal file
View file

@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
#
# gPodder - A media aggregator and podcast client
# Copyright (c) 2005-2009 Thomas Perl and the gPodder Team
#
# gPodder is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# gPodder is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Run Doctests and Unittests for gPodder modules
# 2009-02-25 Thomas Perl <thp@gpodder.org>
import doctest
import unittest
import gettext
# Which package and which modules in the package should be tested?
package = 'gpodder'
modules = ['util']
suite = unittest.TestSuite()
for module in modules:
m = __import__('.'.join((package, module)), fromlist=[module])
# Emulate a globally-installed no-op gettext _() function
if not hasattr(m, '_'):
setattr(m, '_', lambda x: x)
suite.addTest(doctest.DocTestSuite(m))
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)

View file

@ -256,7 +256,7 @@ def file_age_to_string(days):
''
>>> file_age_to_string(1)
'one day ago'
>>> file_age_to_String(2)
>>> file_age_to_string(2)
'2 days ago'
"""
if days == 1:
@ -427,6 +427,40 @@ def extension_from_mimetype(mimetype):
"""
return mimetypes.guess_extension(mimetype) or ''
def extension_correct_for_mimetype(extension, mimetype):
"""
Check if the given filename extension (e.g. ".ogg") is a possible
extension for a given mimetype (e.g. "application/ogg") and return
a boolean value (True if it's possible, False if not). Also do
>>> extension_correct_for_mimetype('.ogg', 'application/ogg')
True
>>> extension_correct_for_mimetype('.ogv', 'video/ogg')
True
>>> extension_correct_for_mimetype('.ogg', 'audio/mpeg')
False
>>> extension_correct_for_mimetype('mp3', 'audio/mpeg')
Traceback (most recent call last):
...
ValueError: "mp3" is not an extension (missing .)
>>> extension_correct_for_mimetype('.mp3', 'audio mpeg')
Traceback (most recent call last):
...
ValueError: "audio mpeg" is not a mimetype (missing /)
"""
if not '/' in mimetype:
raise ValueError('"%s" is not a mimetype (missing /)' % mimetype)
if not extension.startswith('.'):
raise ValueError('"%s" is not an extension (missing .)' % extension)
# Create a "default" extension from the mimetype, e.g. "application/ogg"
# becomes ".ogg", "audio/mpeg" becomes ".mpeg", etc...
default = ['.'+mimetype.split('/')[-1]]
return extension in default+mimetypes.guess_all_extensions(mimetype)
def filename_from_url(url):
"""
Extracts the filename and (lowercase) extension (with dot)
@ -843,7 +877,7 @@ def format_seconds_to_hour_min_sec(seconds):
>>> format_seconds_to_hour_min_sec(3834)
'1 hour, 3 minutes and 54 seconds'
>>> format_seconds_to_hour_min_sec(2600)
>>> format_seconds_to_hour_min_sec(3600)
'1 hour'
>>> format_seconds_to_hour_min_sec(62)
'1 minute and 2 seconds'