A Discrete-Event Network Simulator
API
TestBase.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 ## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
3 #
4 # Copyright (c) 2014 Siddharth Santurkar
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 2 as
8 # published by the Free Software Foundation;
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #
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 
53  """
54  Generic class for testing tools based on provided commands and test cases.
55  """
56 
57  def __init__(self, argv, desc, mode):
58  """
59  Provide input argument list, description and mode of the suite being executed.
60  """
61  self.my_env = os.environ
62  set_workdir()
63  self.my_env['LD_LIBRARY_PATH'] = os.getcwd() + "/build"
64  self.mode = mode
65  self.outfile = 'test-port-'+self.mode+'.out'
66  self.options = self.parseargs(argv , desc)
67 
68  def parseargs(self, argv, desc):
69  """
70  Parses the commandline arguments
71  """
72  parser = argparse.ArgumentParser(description = desc)
73  parser.add_argument('-f', '--file', action='store', dest='out_file', default = self.outfile,
74  metavar="FILE",
75  help='File to be used for storing the command specific output (Default: '+self.outfile+')')
76  parser.add_argument('-c', action='store_true', dest='cmds', default=False,
77  help='List out all the commands being tested')
78  parser.add_argument('-m', action='store_true', dest='mute', default=False,
79  help='Sends only stderr output to FILE')
80  parser.add_argument('-x', '--customcmd', action='store', dest='custcmd', default = None,
81  help='Enter a comma-separated list of commands to override the existing ones. NOT APPLICABLE FOR TEST-PY SUITE.')
82  return parser.parse_args(argv)
83 
84  def override_cmds(self):
85  """
86  Can be used by importing suite to handle custom commands
87  """
88  return self.options.custcmd
89 
90  def runtests(self, cmds):
91  """
92  Execute the tests.
93  """
94  if self.options.cmds:
95  print_cmds(cmds)
96  return
97 
98  final_return = 0
99  total_tests = len(cmds)
100  passed = 0
101  progress = 0.0
102  failed_cases = []
103  with open(self.options.out_file, 'w') as out:
104  outstream = out
105  with open(os.devnull, 'w') as sink:
106  if self.options.mute:
107  outstream = sink
108  for cmd in cmds:
109  case_string = cmd.replace(sys.executable, '')
110  print("running test case: " + case_string)
111  print_case_in_file(case_string, out)
112  progress += 1
113  ret = subprocess.call(cmd, shell=True, env=self.my_env, stdout=outstream, stderr=out)
114  if not ret:
115  passed += 1
116  else:
117  final_return = 1
118  failed_cases.append(case_string)
119  print("[ %s out of %s ] test cases passed; Progress = %.2f%% \n" % (passed, total_tests, progress*100/total_tests))
120  if final_return != 0:
121  print_failed_cases(failed_cases)
122  else:
123  print("\nAll cases passed")
124  print ("Detailed output available in " + self.options.out_file, end='\n\n')
125  return final_return
def print_failed_cases(failed_cases)
Definition: TestBase.py:33
def runtests(self, cmds)
Definition: TestBase.py:90
def set_workdir()
Definition: TestBase.py:43
def override_cmds(self)
Definition: TestBase.py:84
def parseargs(self, argv, desc)
Definition: TestBase.py:68
def print_case_in_file(case_string, out)
Definition: TestBase.py:26
def __init__(self, argv, desc, mode)
Definition: TestBase.py:57
def print_cmds(cmds)
Definition: TestBase.py:38