run_tests.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 ########################################################################
3 # A Python script to run one or more tests from the test/ directory
4 #
5 # 2014 O. Korobkin
6 ########################################################################
7 
8 import os, sys, optparse
9 import time, subprocess
10 from testcase_class import *
11 
12 #--- check arguments and print help message if no arguments given ------
13 parse = optparse.OptionParser()
14 parse.set_usage("""
15  ./run_tests.py [--keep|-k] [--help|-h] t1 [t2 [...]]
16 
17  - unpack and launch test[s] in the test/ directory.
18 
19 Arguments:
20  t1 [t2 [..]]: tests to run """)
21 
22 #--- parse options and arguments ---------------------------------------
23 parse.add_option("-k","--keep",action="store_false", \
24  dest="cleanup_after_test", default=True, \
25  help="do not erase test directory after it succeeded")
26 
27 (options, args) = parse.parse_args()
28 
29 if len(args) < 1:
30  print("ERROR: Wrong argument string")
31  parse.print_help()
32  sys.exit(1)
33 
34 #--- source and base directories --------------------------------------
35 maindir = os.getcwd()
36 program = maindir+"/bin/winnet"
37 testdir = maindir+"/test"
38 bindir = maindir+"/bin"
39 if not os.path.isdir(testdir): # check if the test directory exists
40  print("ERROR: test directory does not exist. Perhaps you are not running")
41  print(" this script from the WINNET root directory with the ")
42  print(" `make tests` command.")
43  sys.exit(1)
44 
45 #--- main loop ----------------------------------------------------
46 print("Running tests:")
47 n_tests = len(args)
48 n_failed = 0
49 for i in range(len(args)):
50  # skip options
51  tc= args[i]
52  if tc[0]=='-':
53  if not (tc=="-k" or tc=="--keep"):
54  print ("ERROR: unknown option.")
55  parse.print_help()
56  continue
57 
58  t = testcase(tc, maindir)
59  nd = 3 if len(args)>99 else 2 if len(args)>9 else 1
60  wt_format = "%" + str(nd) + "d/%" + str(nd) + "d"
61  t.whichtest = wt_format % (i+1,len(args))
62  t.len_spaces = 45 - len(tc)
63  if not os.path.exists(testdir + "/" + tc + ".py"):
64  print("ERROR: testcase "+tc+" does not exist!")
65  n_failed += 1
66  continue
67  exec(compile(open(testdir + "/" + tc + ".py", "rb").read(), testdir + "/" + tc + ".py", 'exec'))
68 
69  res= t.deploy()
70  if res!=0:
71  print("ERROR: while unpacking test "+tc)
72  n_failed += 1
73  continue
74 
75  # Enable a callback function for some tests
76  try:
77  # Method exists and was used.
78  t.prepare_function()
79  except AttributeError:
80  # If the method does not exist
81  pass
82 
83  res= t.launch()
84  if res!=0:
85  print("ERROR: when launching test "+tc)
86  n_failed += 1
87  continue
88 
89  t.monitor()
90  res= t.analyze()
91  if res!=0:
92  print("\r [" + t.whichtest + "] " + tc \
93  + ": .."+"."*t.len_spaces + "... [FAIL] ")
94  n_failed += 1
95  else:
96  print("\r [" + t.whichtest + "] " + tc \
97  + ": .."+"."*t.len_spaces + "... [ OK ] ")
98  if options.cleanup_after_test:
99  t.cleanup() # only if passed
100 
101 if n_failed>0:
102  print(" - %d out of %d tests have failed." % (n_failed, n_tests))
103 else:
104  print(" - all tests have passed successfully!")
bin.testcase_class.testcase
Definition: testcase_class.py:303