From c84f45a751bc1371b64021a72cae441c7a849d8e Mon Sep 17 00:00:00 2001 From: Marcel Hellkamp Date: Sun, 12 Jun 2022 20:53:45 +0200 Subject: fix: Make tests runnable with both unittest and pytest The old test method (test/testall.py) was broken. Travis was removed, because it does not support py25 anymore anyway. --- .travis.yml | 20 -------------------- Makefile | 31 ++++--------------------------- test/__init__.py | 11 +++++++++++ test/test_auth.py | 2 +- test/test_environ.py | 2 +- test/test_jinja2.py | 2 +- test/test_mako.py | 2 +- test/test_mount.py | 2 +- test/test_outputfilter.py | 2 +- test/test_plugins.py | 3 +-- test/test_route.py | 2 +- test/test_sendfile.py | 33 +++++++++++++++++---------------- test/test_server.py | 4 ++-- test/test_stpl.py | 18 ++++++++++-------- test/test_wsgi.py | 5 +++-- test/testall.py | 45 --------------------------------------------- tox.ini | 2 +- 17 files changed, 56 insertions(+), 130 deletions(-) delete mode 100644 .travis.yml create mode 100644 test/__init__.py delete mode 100755 test/testall.py diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 1e125a3..0000000 --- a/.travis.yml +++ /dev/null @@ -1,20 +0,0 @@ -language: python - -python: - - "2.6" - - "2.7" - - "3.2" - - "3.3" - - "3.4" - -matrix: - include: - - python: 2.7 - env: PY=2.5 - - python: 2.7 - env: PY=3.1 - -install: - - bash test/travis_setup.sh - -script: python${PY:-} test/testall.py fast diff --git a/Makefile b/Makefile index 715eb46..060a6b1 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ VERSION = $(shell python setup.py --version) ALLFILES = $(shell echo bottle.py test/*.py test/views/*.tpl) VENV = build/venv -.PHONY: release coverage install docs test test_all test_25 test_26 test_27 test_31 test_32 test_33 2to3 clean +.PHONY: release coverage install docs test 2to3 clean release: clean venv $(VENV)/bin/python3 setup.py --version | egrep -q -v '[a-zA-Z]' # Fail on dev/rc versions @@ -25,12 +25,12 @@ $(VENV)/.installed: Makefile coverage: -mkdir build/ coverage erase - COVERAGE_PROCESS_START=.coveragerc test/testall.py + COVERAGE_PROCESS_START=.coveragerc python -m unittest discover coverage combine coverage report coverage html -push: test_all +push: test git push origin HEAD install: @@ -40,30 +40,7 @@ docs: sphinx-build -b html -d build/docs/doctrees docs build/docs/html test: - python test/testall.py - -test_all: test_25 test_26 test_27 test_31 test_32 test_33 test_34 - -test_25: - python2.5 test/testall.py - -test_26: - python2.6 test/testall.py - -test_27: - python2.7 test/testall.py - -test_31: - python3.1 test/testall.py - -test_32: - python3.2 test/testall.py - -test_33: - python3.3 test/testall.py - -test_34: - python3.4 test/testall.py + python -m unittest discover clean: rm -rf build/ dist/ MANIFEST 2>/dev/null || true diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..7861018 --- /dev/null +++ b/test/__init__.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +try: + import coverage + coverage.process_startup() +except ImportError: + pass + +import bottle +bottle.debug(True) diff --git a/test/test_auth.py b/test/test_auth.py index e07ffb5..d3592f6 100644 --- a/test/test_auth.py +++ b/test/test_auth.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- import bottle -from tools import ServerTestBase +from .tools import ServerTestBase class TestBasicAuth(ServerTestBase): diff --git a/test/test_environ.py b/test/test_environ.py index 9680dd1..de50c0b 100755 --- a/test/test_environ.py +++ b/test/test_environ.py @@ -6,7 +6,7 @@ import sys import bottle from bottle import request, tob, touni, tonat, json_dumps, _e, HTTPError, parse_date -import tools +from . import tools import wsgiref.util import base64 diff --git a/test/test_jinja2.py b/test/test_jinja2.py index 0653dfc..6cae4fb 100644 --- a/test/test_jinja2.py +++ b/test/test_jinja2.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import unittest from bottle import Jinja2Template, jinja2_template, jinja2_view, touni -from tools import warn +from .tools import warn class TestJinja2Template(unittest.TestCase): diff --git a/test/test_mako.py b/test/test_mako.py index cedba78..89f10b6 100644 --- a/test/test_mako.py +++ b/test/test_mako.py @@ -1,5 +1,5 @@ import unittest -from tools import warn +from .tools import warn from bottle import MakoTemplate, mako_template, mako_view, touni class TestMakoTemplate(unittest.TestCase): diff --git a/test/test_mount.py b/test/test_mount.py index 0065f20..5a0f4d1 100644 --- a/test/test_mount.py +++ b/test/test_mount.py @@ -1,5 +1,5 @@ import bottle -from tools import ServerTestBase +from .tools import ServerTestBase from bottle import response class TestAppMounting(ServerTestBase): diff --git a/test/test_outputfilter.py b/test/test_outputfilter.py index f248224..4eb16f1 100755 --- a/test/test_outputfilter.py +++ b/test/test_outputfilter.py @@ -4,7 +4,7 @@ import unittest import bottle from bottle import tob, touni -from tools import ServerTestBase, tobs, warn +from .tools import ServerTestBase, tobs, warn class TestOutputFilter(ServerTestBase): ''' Tests for WSGI functionality, routing and output casting (decorators) ''' diff --git a/test/test_plugins.py b/test/test_plugins.py index 9fbcf4a..aec5042 100644 --- a/test/test_plugins.py +++ b/test/test_plugins.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- import unittest -import tools - +from . import tools class MyPlugin(object): def __init__(self): diff --git a/test/test_route.py b/test/test_route.py index 66709cb..215a03c 100644 --- a/test/test_route.py +++ b/test/test_route.py @@ -1,6 +1,6 @@ import unittest import bottle -from tools import api +from .tools import api class TestRoute(unittest.TestCase): diff --git a/test/test_sendfile.py b/test/test_sendfile.py index 1d63b6e..68e6447 100755 --- a/test/test_sendfile.py +++ b/test/test_sendfile.py @@ -5,6 +5,9 @@ import os import tempfile import time +basename = os.path.basename(__file__) +root = os.path.dirname(__file__) + class TestDateParser(unittest.TestCase): def test_rfc1123(self): """DateParser: RFC 1123 format""" @@ -39,13 +42,13 @@ class TestSendFile(unittest.TestCase): def test_valid(self): """ SendFile: Valid requests""" - out = static_file(os.path.basename(__file__), root='./') + out = static_file(basename, root=root) self.assertEqual(open(__file__,'rb').read(), out.body.read()) def test_invalid(self): """ SendFile: Invalid requests""" - self.assertEqual(404, static_file('not/a/file', root='./').status_code) - f = static_file(os.path.join('./../', os.path.basename(__file__)), root='./views/') + self.assertEqual(404, static_file('not/a/file', root=root).status_code) + f = static_file(os.path.join('./../', basename), root='./views/') self.assertEqual(403, f.status_code) try: fp, fn = tempfile.mkstemp() @@ -57,41 +60,39 @@ class TestSendFile(unittest.TestCase): def test_mime(self): """ SendFile: Mime Guessing""" - f = static_file(os.path.basename(__file__), root='./') + f = static_file(basename, root=root) self.assertTrue(f.headers['Content-Type'].split(';')[0] in ('application/x-python-code', 'text/x-python')) - f = static_file(os.path.basename(__file__), root='./', mimetype='some/type') + f = static_file(basename, root=root, mimetype='some/type') self.assertEqual('some/type', f.headers['Content-Type']) - f = static_file(os.path.basename(__file__), root='./', mimetype='text/foo') + f = static_file(basename, root=root, mimetype='text/foo') self.assertEqual('text/foo; charset=UTF-8', f.headers['Content-Type']) - f = static_file(os.path.basename(__file__), root='./', mimetype='text/foo', charset='latin1') + f = static_file(basename, root=root, mimetype='text/foo', charset='latin1') self.assertEqual('text/foo; charset=latin1', f.headers['Content-Type']) def test_ims(self): """ SendFile: If-Modified-Since""" request.environ['HTTP_IF_MODIFIED_SINCE'] = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime()) - res = static_file(os.path.basename(__file__), root='./') + res = static_file(basename, root=root) self.assertEqual(304, res.status_code) self.assertEqual(int(os.stat(__file__).st_mtime), parse_date(res.headers['Last-Modified'])) self.assertAlmostEqual(int(time.time()), parse_date(res.headers['Date'])) request.environ['HTTP_IF_MODIFIED_SINCE'] = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(100)) - self.assertEqual(open(__file__,'rb').read(), static_file(os.path.basename(__file__), root='./').body.read()) + self.assertEqual(open(__file__,'rb').read(), static_file(basename, root=root).body.read()) def test_download(self): """ SendFile: Download as attachment """ - basename = os.path.basename(__file__) - f = static_file(basename, root='./', download=True) + f = static_file(basename, root=root, download=True) self.assertEqual('attachment; filename="%s"' % basename, f.headers['Content-Disposition']) request.environ['HTTP_IF_MODIFIED_SINCE'] = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(100)) - f = static_file(os.path.basename(__file__), root='./') + f = static_file(basename, root=root) self.assertEqual(open(__file__,'rb').read(), f.body.read()) def test_range(self): - basename = os.path.basename(__file__) request.environ['HTTP_RANGE'] = 'bytes=10-25,-80' - f = static_file(basename, root='./') - c = open(basename, 'rb'); c.seek(10) + f = static_file(basename, root=root) + c = open(__file__, 'rb'); c.seek(10) self.assertEqual(c.read(16), tob('').join(f.body)) - self.assertEqual('bytes 10-25/%d' % len(open(basename, 'rb').read()), + self.assertEqual('bytes 10-25/%d' % len(open(__file__, 'rb').read()), f.headers['Content-Range']) self.assertEqual('bytes', f.headers['Accept-Ranges']) diff --git a/test/test_server.py b/test/test_server.py index aa25a66..b6ff9a2 100644 --- a/test/test_server.py +++ b/test/test_server.py @@ -1,13 +1,13 @@ # -*- coding: utf-8 -*- import unittest import time -from tools import tob +from .tools import tob import sys import os import signal import socket from subprocess import Popen, PIPE -import tools +from . import tools from bottle import _e try: diff --git a/test/test_stpl.py b/test/test_stpl.py index bd39a1d..32b53be 100755 --- a/test/test_stpl.py +++ b/test/test_stpl.py @@ -1,13 +1,15 @@ # -*- coding: utf-8 -*- import unittest from bottle import SimpleTemplate, TemplateError, view, template, touni, tob, html_quote -import re +import re, os import traceback +views_dir = os.path.join(os.path.dirname(__file__), 'views') + class TestSimpleTemplate(unittest.TestCase): def assertRenders(self, tpl, to, *args, **vars): if isinstance(tpl, str): - tpl = SimpleTemplate(tpl) + tpl = SimpleTemplate(tpl, lookup=[views_dir]) self.assertEqual(touni(to), tpl.render(*args, **vars)) def test_string(self): @@ -18,11 +20,11 @@ class TestSimpleTemplate(unittest.TestCase): self.assertRenders('start {{self}} end', 'start var end', {'self':'var'}) def test_file(self): - t = SimpleTemplate(name='./views/stpl_simple.tpl') + t = SimpleTemplate(name=views_dir + os.sep + 'stpl_simple.tpl') self.assertRenders(t, 'start var end\n', var='var') def test_name(self): - t = SimpleTemplate(name='stpl_simple', lookup=['./views/']) + t = SimpleTemplate(name='stpl_simple', lookup=[views_dir]) self.assertRenders(t, 'start var end\n', var='var') def test_unicode(self): @@ -31,7 +33,7 @@ class TestSimpleTemplate(unittest.TestCase): def test_unicode_code(self): """ Templates: utf8 code in file""" - t = SimpleTemplate(name='./views/stpl_unicode.tpl') + t = SimpleTemplate(name='stpl_unicode.tpl', lookup=[views_dir]) self.assertRenders(t, 'start ñç äöü end\n', var=touni('äöü')) def test_import(self): @@ -148,12 +150,12 @@ class TestSimpleTemplate(unittest.TestCase): def test_include(self): """ Templates: Include statements""" - t = SimpleTemplate(name='stpl_include', lookup=['./views/']) + t = SimpleTemplate(name='stpl_include', lookup=[views_dir]) self.assertRenders(t, 'before\nstart var end\nafter\n', var='var') def test_rebase(self): """ Templates: %rebase and method passing """ - t = SimpleTemplate(name='stpl_t2main', lookup=['./views/']) + t = SimpleTemplate(name='stpl_t2main', lookup=[views_dir]) result='+base+\n+main+\n!1234!\n+include+\n-main-\n+include+\n-base-\n' self.assertRenders(t, result, content='1234') @@ -233,7 +235,7 @@ class TestSimpleTemplate(unittest.TestCase): self.assertEqual(touni('start middle end'), test()) def test_view_decorator_issue_407(self): - @view('stpl_no_vars') + @view('stpl_no_vars', template_lookup=[views_dir]) def test(): pass self.assertEqual(touni('hihi'), test()) diff --git a/test/test_wsgi.py b/test/test_wsgi.py index 72563fc..973ae8e 100755 --- a/test/test_wsgi.py +++ b/test/test_wsgi.py @@ -1,8 +1,9 @@ # -*- coding: utf-8 -*- import unittest import bottle -from tools import ServerTestBase +from .tools import ServerTestBase from bottle import tob +import os.path class TestWsgi(ServerTestBase): ''' Tests for WSGI functionality, routing and output casting (decorators) ''' @@ -274,7 +275,7 @@ class TestDecorators(ServerTestBase): def test_view(self): """ WSGI: Test view-decorator (should override autojson) """ @bottle.route('/tpl') - @bottle.view('stpl_t2main') + @bottle.view('stpl_t2main', template_lookup=[os.path.join(os.path.dirname(__file__), 'views')]) def test(): return dict(content='1234') result = '+base+\n+main+\n!1234!\n+include+\n-main-\n+include+\n-base-\n' diff --git a/test/testall.py b/test/testall.py deleted file mode 100755 index 32e5a90..0000000 --- a/test/testall.py +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -try: - import coverage - coverage.process_startup() -except ImportError: - pass - -import unittest -import sys, os, glob - -test_root = os.path.dirname(os.path.abspath(__file__)) -test_files = glob.glob(os.path.join(test_root, 'test_*.py')) - -os.chdir(test_root) -sys.path.insert(0, os.path.dirname(test_root)) -sys.path.insert(0, test_root) -test_names = [os.path.basename(name)[:-3] for name in test_files] - -if 'help' in sys.argv or '-h' in sys.argv: - sys.stdout.write('''Command line arguments: - fast: Skip server adapter tests. - verbose: Print tests even if they pass. - ''') - sys.exit(0) - -if 'fast' in sys.argv: - sys.stderr.write("Warning: The 'fast' keyword skipps server tests.\n") - test_names.remove('test_server') - -suite = unittest.defaultTestLoader.loadTestsFromNames(test_names) - -def run(): - import bottle - - bottle.debug(True) - vlevel = 2 if 'verbose' in sys.argv else 0 - result = unittest.TextTestRunner(verbosity=vlevel).run(suite) - - sys.exit((result.errors or result.failures) and 1 or 0) - -if __name__ == '__main__': - run() - diff --git a/tox.ini b/tox.ini index 8337ac7..6d7fe3c 100644 --- a/tox.ini +++ b/tox.ini @@ -4,7 +4,7 @@ envlist = py25-empty,py26,py27,py32,py33,py27-most [testenv] deps=Mako jinja2 -commands={envpython} test/testall.py +commands={envpython} -m unittest discover sitepackages=False [testenv:py25-empty] -- cgit v1.2.1