A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
TestBase.py
Go to the documentation of this file.
1
#! /usr/bin/env python3
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
43
def
set_workdir
():
44
dir_files = [ f
for
f
in
os.listdir(
'.'
)
if
os.path.exists(f) ]
45
if
not
'VERSION'
in
dir_files
and
not
'ns3'
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
53
class
TestBaseClass
:
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
"""
74
self.
my_env
my_env = os.environ
75
set_workdir
()
76
self.
my_env
my_env[
'LD_LIBRARY_PATH'
] = os.getcwd() +
"/build"
77
self.
mode
mode = mode
78
self.
outfile
outfile =
'test-port-'
+self.
mode
mode+
'.out'
79
self.
options
options = self.
parseargs
parseargs(argv , desc)
80
81
def
parseargs
(self, argv, desc):
82
"""!
83
Parses the commandline arguments
84
@param self this object
85
@param argv argument list
86
@param desc description
87
@return command line arguments
88
"""
89
parser = argparse.ArgumentParser(description = desc)
90
parser.add_argument(
'-f'
,
'--file'
, action=
'store'
, dest=
'out_file'
, default = self.
outfile
outfile,
91
metavar=
"FILE"
,
92
help=
'File to be used for storing the command specific output (Default: '
+self.
outfile
outfile+
')'
)
93
parser.add_argument(
'-c'
, action=
'store_true'
, dest=
'cmds'
, default=
False
,
94
help=
'List out all the commands being tested'
)
95
parser.add_argument(
'-m'
, action=
'store_true'
, dest=
'mute'
, default=
False
,
96
help=
'Sends only stderr output to FILE'
)
97
parser.add_argument(
'-x'
,
'--customcmd'
, action=
'store'
, dest=
'custcmd'
, default =
None
,
98
help=
'Enter a comma-separated list of commands to override the existing ones. NOT APPLICABLE FOR TEST-PY SUITE.'
)
99
return
parser.parse_args(argv)
100
101
def
override_cmds
(self):
102
"""!
103
Can be used by importing suite to handle custom commands
104
@param self this object
105
@return custom commands
106
"""
107
return
self.
options
options.custcmd
108
109
def
runtests
(self, cmds):
110
"""!
111
Execute the tests.
112
@param self this object
113
@param cmds test commands
114
@return error code
115
"""
116
if
self.
options
options.cmds:
117
print_cmds
(cmds)
118
return
119
120
final_return = 0
121
total_tests = len(cmds)
122
passed = 0
123
progress = 0.0
124
failed_cases = []
125
with
open(self.
options
options.out_file,
'w'
)
as
out:
126
outstream = out
127
with
open(os.devnull,
'w'
)
as
sink:
128
if
self.
options
options.mute:
129
outstream = sink
130
for
cmd
in
cmds:
131
case_string = cmd.replace(sys.executable,
''
)
132
print(
"running test case: "
+ case_string)
133
print_case_in_file
(case_string, out)
134
progress += 1
135
ret = subprocess.call(cmd, shell=
True
, env=self.
my_env
my_env, stdout=outstream, stderr=out)
136
if
not
ret:
137
passed += 1
138
else
:
139
final_return = 1
140
failed_cases.append(case_string)
141
print(
"[ %s out of %s ] test cases passed; Progress = %.2f%% \n"
% (passed, total_tests, progress*100/total_tests))
142
if
final_return != 0:
143
print_failed_cases
(failed_cases)
144
else
:
145
print(
"\nAll cases passed"
)
146
print
(
"Detailed output available in "
+ self.
options
options.out_file, end=
'\n\n'
)
147
return
final_return
TestBase.TestBaseClass
TestBaseClass class.
Definition:
TestBase.py:53
TestBase.TestBaseClass.__init__
def __init__(self, argv, desc, mode)
Provide input argument list, description and mode of the suite being executed.
Definition:
TestBase.py:66
TestBase.TestBaseClass.parseargs
def parseargs(self, argv, desc)
Parses the commandline arguments.
Definition:
TestBase.py:81
TestBase.TestBaseClass.override_cmds
def override_cmds(self)
Can be used by importing suite to handle custom commands.
Definition:
TestBase.py:101
TestBase.TestBaseClass.mode
mode
mode
Definition:
TestBase.py:77
TestBase.TestBaseClass.options
options
options
Definition:
TestBase.py:79
TestBase.TestBaseClass.outfile
outfile
output file
Definition:
TestBase.py:78
TestBase.TestBaseClass.runtests
def runtests(self, cmds)
Execute the tests.
Definition:
TestBase.py:109
TestBase.TestBaseClass.my_env
my_env
os environment
Definition:
TestBase.py:74
TestBase.print_case_in_file
def print_case_in_file(case_string, out)
Definition:
TestBase.py:26
TestBase.print_cmds
def print_cmds(cmds)
Definition:
TestBase.py:38
TestBase.print_failed_cases
def print_failed_cases(failed_cases)
Definition:
TestBase.py:33
TestBase.set_workdir
def set_workdir()
Definition:
TestBase.py:43
utils
tests
TestBase.py
Generated on Tue Feb 6 2024 19:21:31 for ns-3 by
1.9.1