A Discrete-Event Network Simulator
API
TestBase.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
19 
20 from __future__ import print_function
21 import sys
22 import subprocess
23 import argparse
24 import os
25 
26 def print_case_in_file(case_string, out):
27  for i in range(100):
28  print("-", end = '', file = out)
29  print(file=out)
30  print("running test case " + case_string, end='\n\n', file = out)
31  out.flush()
32 
33 def print_failed_cases(failed_cases):
34  print("\nFailed Cases:")
35  for case in failed_cases:
36  print(case)
37 
38 def print_cmds(cmds):
39  print('Commands to be executed:')
40  for cmd in cmds:
41  print(cmd.replace(sys.executable, ''))
42 
44  dir_files = [ f for f in os.listdir('.') if os.path.exists(f) ]
45  if not 'VERSION' in dir_files and not 'waf' in dir_files:
46  if os.path.split(os.path.abspath('.'))[1] == 'tests' and os.path.split(os.path.abspath(os.pardir))[1] == 'utils':
47  os.chdir('../../')
48  else:
49  print('Error: Invalid working directory')
50  sys.exit(1)
51 
52 
54  """
55  Generic class for testing tools based on provided commands and test cases.
56  """
57 
65 
66  def __init__(self, argv, desc, mode):
67  """!
68  Provide input argument list, description and mode of the suite being executed.
69  @param self this object
70  @param argv argument list
71  @param desc description
72  @param mode test mode
73  @return none
74  """
75  self.my_env = os.environ
76  set_workdir()
77  self.my_env['LD_LIBRARY_PATH'] = os.getcwd() + "/build"
78  self.mode = mode
79  self.outfile = 'test-port-'+self.mode+'.out'
80  self.options = self.parseargs(argv , desc)
81 
82  def parseargs(self, argv, desc):
83  """!
84  Parses the commandline arguments
85  @param self this object
86  @param argv argument list
87  @param desc description
88  @return command line arguments
89  """
90  parser = argparse.ArgumentParser(description = desc)
91  parser.add_argument('-f', '--file', action='store', dest='out_file', default = self.outfile,
92  metavar="FILE",
93  help='File to be used for storing the command specific output (Default: '+self.outfile+')')
94  parser.add_argument('-c', action='store_true', dest='cmds', default=False,
95  help='List out all the commands being tested')
96  parser.add_argument('-m', action='store_true', dest='mute', default=False,
97  help='Sends only stderr output to FILE')
98  parser.add_argument('-x', '--customcmd', action='store', dest='custcmd', default = None,
99  help='Enter a comma-separated list of commands to override the existing ones. NOT APPLICABLE FOR TEST-PY SUITE.')
100  return parser.parse_args(argv)
101 
102  def override_cmds(self):
103  """!
104  Can be used by importing suite to handle custom commands
105  @param self this object
106  @return custom commands
107  """
108  return self.options.custcmd
109 
110  def runtests(self, cmds):
111  """!
112  Execute the tests.
113  @param self this object
114  @param cmds test commands
115  @return error code
116  """
117  if self.options.cmds:
118  print_cmds(cmds)
119  return
120 
121  final_return = 0
122  total_tests = len(cmds)
123  passed = 0
124  progress = 0.0
125  failed_cases = []
126  with open(self.options.out_file, 'w') as out:
127  outstream = out
128  with open(os.devnull, 'w') as sink:
129  if self.options.mute:
130  outstream = sink
131  for cmd in cmds:
132  case_string = cmd.replace(sys.executable, '')
133  print("running test case: " + case_string)
134  print_case_in_file(case_string, out)
135  progress += 1
136  ret = subprocess.call(cmd, shell=True, env=self.my_env, stdout=outstream, stderr=out)
137  if not ret:
138  passed += 1
139  else:
140  final_return = 1
141  failed_cases.append(case_string)
142  print("[ %s out of %s ] test cases passed; Progress = %.2f%% \n" % (passed, total_tests, progress*100/total_tests))
143  if final_return != 0:
144  print_failed_cases(failed_cases)
145  else:
146  print("\nAll cases passed")
147  print ("Detailed output available in " + self.options.out_file, end='\n\n')
148  return final_return
def print_failed_cases(failed_cases)
Definition: TestBase.py:33
def runtests(self, cmds)
Execute the tests.
Definition: TestBase.py:110
my_env
os environment
Definition: TestBase.py:75
outfile
output file
Definition: TestBase.py:79
def set_workdir()
Definition: TestBase.py:43
def override_cmds(self)
Can be used by importing suite to handle custom commands.
Definition: TestBase.py:102
def parseargs(self, argv, desc)
Parses the commandline arguments.
Definition: TestBase.py:82
TestBaseClass class.
Definition: TestBase.py:53
def print_case_in_file(case_string, out)
Definition: TestBase.py:26
def __init__(self, argv, desc, mode)
Provide input argument list, description and mode of the suite being executed.
Definition: TestBase.py:66
def print_cmds(cmds)
Definition: TestBase.py:38