A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
TestBase.py
Go to the documentation of this file.
1#! /usr/bin/env python3
2#
3# Copyright (c) 2014 Siddharth Santurkar
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License version 2 as
7# published by the Free Software Foundation;
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17#
18
19from __future__ import print_function
20import sys
21import subprocess
22import argparse
23import os
24
25def print_case_in_file(case_string, out):
26 for i in range(100):
27 print("-", end = '', file = out)
28 print(file=out)
29 print("running test case " + case_string, end='\n\n', file = out)
30 out.flush()
31
32def print_failed_cases(failed_cases):
33 print("\nFailed Cases:")
34 for case in failed_cases:
35 print(case)
36
37def print_cmds(cmds):
38 print('Commands to be executed:')
39 for cmd in cmds:
40 print(cmd.replace(sys.executable, ''))
41
43 dir_files = [ f for f in os.listdir('.') if os.path.exists(f) ]
44 if not 'VERSION' in dir_files and not 'ns3' in dir_files:
45 if os.path.split(os.path.abspath('.'))[1] == 'tests' and os.path.split(os.path.abspath(os.pardir))[1] == 'utils':
46 os.chdir('../../')
47 else:
48 print('Error: Invalid working directory')
49 sys.exit(1)
50
51
53 """
54 Generic class for testing tools based on provided commands and test cases.
55 """
56
64
65 def __init__(self, argv, desc, mode):
66 """!
67 Provide input argument list, description and mode of the suite being executed.
68 @param self this object
69 @param argv argument list
70 @param desc description
71 @param mode test mode
72 """
73 self.my_env = os.environ
75 self.my_env['LD_LIBRARY_PATH'] = os.getcwd() + "/build"
76 self.mode = mode
77 self.outfile = 'test-port-'+self.mode+'.out'
78 self.options = self.parseargs(argv , desc)
79
80 def parseargs(self, argv, desc):
81 """!
82 Parses the commandline arguments
83 @param self this object
84 @param argv argument list
85 @param desc description
86 @return command line arguments
87 """
88 parser = argparse.ArgumentParser(description = desc)
89 parser.add_argument('-f', '--file', action='store', dest='out_file', default = self.outfile,
90 metavar="FILE",
91 help='File to be used for storing the command specific output (Default: '+self.outfile+')')
92 parser.add_argument('-c', action='store_true', dest='cmds', default=False,
93 help='List out all the commands being tested')
94 parser.add_argument('-m', action='store_true', dest='mute', default=False,
95 help='Sends only stderr output to FILE')
96 parser.add_argument('-x', '--customcmd', action='store', dest='custcmd', default = None,
97 help='Enter a comma-separated list of commands to override the existing ones. NOT APPLICABLE FOR TEST-PY SUITE.')
98 return parser.parse_args(argv)
99
100 def override_cmds(self):
101 """!
102 Can be used by importing suite to handle custom commands
103 @param self this object
104 @return custom commands
105 """
106 return self.options.custcmd
107
108 def runtests(self, cmds):
109 """!
110 Execute the tests.
111 @param self this object
112 @param cmds test commands
113 @return error code
114 """
115 if self.options.cmds:
116 print_cmds(cmds)
117 return
118
119 final_return = 0
120 total_tests = len(cmds)
121 passed = 0
122 progress = 0.0
123 failed_cases = []
124 with open(self.options.out_file, 'w') as out:
125 outstream = out
126 with open(os.devnull, 'w') as sink:
127 if self.options.mute:
128 outstream = sink
129 for cmd in cmds:
130 case_string = cmd.replace(sys.executable, '')
131 print("running test case: " + case_string)
132 print_case_in_file(case_string, out)
133 progress += 1
134 ret = subprocess.call(cmd, shell=True, env=self.my_env, stdout=outstream, stderr=out)
135 if not ret:
136 passed += 1
137 else:
138 final_return = 1
139 failed_cases.append(case_string)
140 print("[ %s out of %s ] test cases passed; Progress = %.2f%% \n" % (passed, total_tests, progress*100/total_tests))
141 if final_return != 0:
142 print_failed_cases(failed_cases)
143 else:
144 print("\nAll cases passed")
145 print ("Detailed output available in " + self.options.out_file, end='\n\n')
146 return final_return
TestBaseClass class.
Definition: TestBase.py:52
def __init__(self, argv, desc, mode)
Provide input argument list, description and mode of the suite being executed.
Definition: TestBase.py:65
def parseargs(self, argv, desc)
Parses the commandline arguments.
Definition: TestBase.py:80
def override_cmds(self)
Can be used by importing suite to handle custom commands.
Definition: TestBase.py:100
outfile
output file
Definition: TestBase.py:77
def runtests(self, cmds)
Execute the tests.
Definition: TestBase.py:108
my_env
os environment
Definition: TestBase.py:73
def print_case_in_file(case_string, out)
Definition: TestBase.py:25
def print_cmds(cmds)
Definition: TestBase.py:37
def print_failed_cases(failed_cases)
Definition: TestBase.py:32
def set_workdir()
Definition: TestBase.py:42
-ns3 Test suite for the ns3 wrapper script