annotate run-tests @ 71:88adf10be709 draft

Add tests.
author David Barts <n5jrn@me.com>
date Mon, 15 Jul 2019 13:16:31 -0700
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
71
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
1 #!/usr/bin/env python3
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
2 # Run our tests. Basically, these use the python unittest framework, but
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
3 # are organized into a directory tree containing test code and the server
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
4 # files to run the tests against. The tests are done by creating a local
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
5 # server on the specified port (default 8080) and issuing requests to it.
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
6 #
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
7 # Note that some of the test suites we run require the requests and/or
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
8 # Beautiful Soup libraries (these are not needed for running TInCan, only
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
9 # for testing it.)
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
10
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
11 # I m p o r t s
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
12
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
13 import os, sys
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
14 import importlib
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
15 import logging
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
16 import unittest
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
17 from argparse import ArgumentParser
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
18
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
19 # V a r i a b l e s
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
20
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
21 MYNAME = os.path.basename(sys.argv[0])
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
22 TESTDIR = "tests"
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
23 FPREFIX = "suite_"
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
24 FILESDIR = "files"
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
25 FILESVAR = "files"
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
26 PORTVAR = "port"
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
27
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
28 # F u n c t i o n s
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
29
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
30 def is_test(d):
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
31 if not d.startswith(FPREFIX):
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
32 return False
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
33 return os.path.isdir(os.path.join(TESTDIR, d)) and os.path.isdir(os.path.join(TESTDIR, d, FILESDIR))
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
34
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
35 def is_subclass(a, b):
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
36 try:
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
37 return issubclass(a, b)
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
38 except TypeError:
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
39 return False
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
40
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
41 # M a i n P r o g r a m
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
42
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
43 # Parse arguments
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
44 parser = ArgumentParser(prog=sys.argv[0], usage="%(prog)s [options]")
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
45 parser.add_argument("-p", "--port", default=8080, help="port to use (default: 8080)")
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
46 args = parser.parse_args(sys.argv[1:])
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
47
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
48 # Reject attempts to run out of directory
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
49 if not os.path.isdir(TESTDIR):
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
50 sys.stderr.write("{0}: {1!r} - no such directory\n".format(MYNAME, TESTDIR))
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
51 sys.exit(2)
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
52
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
53 # Get the log file name and ensure the log file is cleared
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
54 from tests import LOGNAME, LOGGERNAME
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
55 open(LOGNAME, "w").close()
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
56
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
57 # Make a logger object for ServerFixture to use
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
58 mylog = logging.getLogger(LOGGERNAME)
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
59 handler = logging.FileHandler(LOGNAME)
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
60 handler.setFormatter(
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
61 logging.Formatter(fmt="%(asctime)s - %(levelname)s: %(message)s"))
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
62 mylog.addHandler(handler)
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
63 mylog.setLevel(logging.INFO)
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
64
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
65 # Locate all fixtures and operate on them
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
66 failures = 0
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
67 fixtures = sorted(filter(is_test, os.listdir(TESTDIR)))
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
68 if not fixtures:
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
69 sys.stderr.write("{0}: warning - no fixtures found\n".format(MYNAME))
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
70 for fixture in fixtures:
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
71 name = "{0}.{1}".format(TESTDIR, fixture)
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
72 sys.stderr.write("{0}: running {1} ...\n".format(MYNAME, name))
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
73 module = importlib.import_module(name)
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
74 directory = os.path.join(TESTDIR, fixture, FILESDIR)
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
75 for i in dir(module):
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
76 v = getattr(module, i)
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
77 if is_subclass(v, unittest.TestCase):
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
78 setattr(v, FILESVAR, directory)
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
79 setattr(v, PORTVAR, args.port)
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
80 suite = unittest.defaultTestLoader.loadTestsFromModule(module)
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
81 result = unittest.TextTestRunner(verbosity=1).run(suite)
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
82 if result.wasSuccessful():
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
83 sys.stderr.write("{0}: {1} passed\n".format(MYNAME, name))
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
84 else:
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
85 sys.stderr.write("{0}: {1} failed\n".format(MYNAME, name))
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
86 failures += 1
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
87
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
88 # AMF...
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
89 if failures:
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
90 sys.stderr.write("{0}: {1} suite{2} failed\n".format(
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
91 MYNAME,
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
92 failures,
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
93 "" if failures == 1 else "s"))
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
94
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
95 sys.stdout.write("Note: server log output is in {0!r}.\n".format(LOGNAME))
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
96 sys.exit(1 if failures else 0)