blob: 79d9b11553ec39dc8bce43f08fd3c4684cbe3158 [file] [log] [blame]
Guido van Rossum85f18201992-11-27 22:53:50 +00001# Automatic Python regression test.
2#
3# Some essential parts of the Python interpreter are tested by the module
4# 'testall'. (Despite its name, it doesn't test everything -- that would
5# be a truly Herculean task!) When a test fails, 'testall' raises an
6# exception. When all tests succeed, it produces quite a lot of output.
7#
8# For a normal regression test, this output is never looked at unless
9# something goes wrong. Thus, it would be wise to suppress the output
10# normally. This module does that, but it doesn't just throw the output
11# from 'testall' away -- it compares it with the output from a previous
12# run. If a difference is noticed it raises an exception; if all is well,
13# it prints nothing except 'All tests OK.' at the very end.
14#
15# The output from a previous run is supposed to be in a file 'testall.out'
16# somewhere on the search path for modules (sys.path, initialized from
17# $PYTHONPATH plus some default places).
18#
19# Of course, if the normal output of the tests is changed because the
20# tests have been changed (rather than a test producing the wrong output),
21# 'autotest' will fail as well. In this case, run 'testall' manually
22# and direct its output to 'testall.out'.
23#
24# The comparison uses (and demonstrates!) a rather new Python feature:
25# program output that normally goes to stdout (by 'print' statements
26# or by writing directly to sys.stdout) can be redirected to an
27# arbitrary class instance as long as it has a 'write' method.
28
29import os
30import sys
31
32# Function to find a file somewhere on sys.path
33def findfile(filename):
34 for dirname in sys.path:
35 fullname = os.path.join(dirname, filename)
36 if os.path.exists(fullname):
37 return fullname
38 return filename # Will cause exception later
39
40# Exception raised when the test failed (not the same as in test_support)
41TestFailed = 'autotest.TestFailed'
42
43# Class substituted for sys.stdout, to compare it with the given file
44class Compare:
Guido van Rossum7bc817d1993-12-17 15:25:27 +000045 def __init__(self, filename):
Guido van Rossum85f18201992-11-27 22:53:50 +000046 self.fp = open(filename, 'r')
Guido van Rossum85f18201992-11-27 22:53:50 +000047 def write(self, data):
48 expected = self.fp.read(len(data))
49 if data <> expected:
50 raise TestFailed, \
51 'Writing: '+`data`+', expected: '+`expected`
52 def close(self):
53 self.fp.close()
54
55# The main program
56def main():
57 import sys
58 filename = findfile('testall.out')
59 real_stdout = sys.stdout
60 try:
Guido van Rossum7bc817d1993-12-17 15:25:27 +000061 sys.stdout = Compare(filename)
Guido van Rossum85f18201992-11-27 22:53:50 +000062 import testall
63 finally:
64 sys.stdout = real_stdout
65 print 'All tests OK.'
66
67main()