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# SPDX-License-Identifier: GPL-2.0-only
6#
7
8from __future__ import print_function
9
10import argparse
11import os
12import subprocess
13import sys
14
15
16def print_case_in_file(case_string, out):
17 for i in range(100):
18 print("-", end="", file=out)
19 print(file=out)
20 print("running test case " + case_string, end="\n\n", file=out)
21 out.flush()
22
23
24def print_failed_cases(failed_cases):
25 print("\nFailed Cases:")
26 for case in failed_cases:
27 print(case)
28
29
30def print_cmds(cmds):
31 print("Commands to be executed:")
32 for cmd in cmds:
33 print(cmd.replace(sys.executable, ""))
34
35
37 dir_files = [f for f in os.listdir(".") if os.path.exists(f)]
38 if not "VERSION" in dir_files and not "ns3" in dir_files:
39 if (
40 os.path.split(os.path.abspath("."))[1] == "tests"
41 and os.path.split(os.path.abspath(os.pardir))[1] == "utils"
42 ):
43 os.chdir("../../")
44 else:
45 print("Error: Invalid working directory")
46 sys.exit(1)
47
48
49## TestBaseClass class
51 """
52 Generic class for testing tools based on provided commands and test cases.
53 """
54
55 ## @var my_env
56 # os environment
57 ## @var mode
58 # mode
59 ## @var outfile
60 # output file
61 ## @var options
62 # options
63
64 def __init__(self, argv, desc, mode):
65 """!
66 Provide input argument list, description and mode of the suite being executed.
67 @param self this object
68 @param argv argument list
69 @param desc description
70 @param mode test mode
71 """
72 self.my_env = os.environ
74 self.my_env["LD_LIBRARY_PATH"] = os.getcwd() + "/build"
75 self.mode = mode
76 self.outfile = "test-port-" + self.mode + ".out"
77 self.options = self.parseargs(argv, desc)
78
79 def parseargs(self, argv, desc):
80 """!
81 Parses the commandline arguments
82 @param self this object
83 @param argv argument list
84 @param desc description
85 @return command line arguments
86 """
87 parser = argparse.ArgumentParser(description=desc)
88 parser.add_argument(
89 "-f",
90 "--file",
91 action="store",
92 dest="out_file",
93 default=self.outfile,
94 metavar="FILE",
95 help="File to be used for storing the command specific output (Default: "
96 + self.outfile
97 + ")",
98 )
99 parser.add_argument(
100 "-c",
101 action="store_true",
102 dest="cmds",
103 default=False,
104 help="List out all the commands being tested",
105 )
106 parser.add_argument(
107 "-m",
108 action="store_true",
109 dest="mute",
110 default=False,
111 help="Sends only stderr output to FILE",
112 )
113 parser.add_argument(
114 "-x",
115 "--customcmd",
116 action="store",
117 dest="custcmd",
118 default=None,
119 help="Enter a comma-separated list of commands to override the existing ones. NOT APPLICABLE FOR TEST-PY SUITE.",
120 )
121 return parser.parse_args(argv)
122
123 def override_cmds(self):
124 """!
125 Can be used by importing suite to handle custom commands
126 @param self this object
127 @return custom commands
128 """
129 return self.options.custcmd
130
131 def runtests(self, cmds):
132 """!
133 Execute the tests.
134 @param self this object
135 @param cmds test commands
136 @return error code
137 """
138 if self.options.cmds:
139 print_cmds(cmds)
140 return
141 base_dir = os.sep.join(
142 os.path.abspath(__file__).replace(os.path.pathsep, "/").split("/")[:-3]
143 )
144 final_return = 0
145 total_tests = len(cmds)
146 passed = 0
147 progress = 0.0
148 failed_cases = []
149 with open(self.options.out_file, "w", encoding="utf-8") as out:
150 outstream = out
151 with open(os.devnull, "w", encoding="utf-8") as sink:
152 if self.options.mute:
153 outstream = sink
154 for cmd in cmds:
155 case_string = cmd.replace(sys.executable, "")
156 print("running test case: " + case_string)
157 print_case_in_file(case_string, out)
158 progress += 1
159 ret = subprocess.call(
160 cmd, shell=True, env=self.my_env, stdout=outstream, stderr=out, cwd=base_dir
161 )
162 if not ret:
163 passed += 1
164 else:
165 final_return = 1
166 failed_cases.append(case_string)
167 print(
168 "[ %s out of %s ] test cases passed; Progress = %.2f%% \n"
169 % (passed, total_tests, progress * 100 / total_tests)
170 )
171 if final_return != 0:
172 print_failed_cases(failed_cases)
173 else:
174 print("\nAll cases passed")
175 print("Detailed output available in " + self.options.out_file, end="\n\n")
176 return final_return
TestBaseClass class.
Definition TestBase.py:50
parseargs(self, argv, desc)
Parses the commandline arguments.
Definition TestBase.py:79
__init__(self, argv, desc, mode)
Provide input argument list, description and mode of the suite being executed.
Definition TestBase.py:64
override_cmds(self)
Can be used by importing suite to handle custom commands.
Definition TestBase.py:123
runtests(self, cmds)
Execute the tests.
Definition TestBase.py:131
my_env
os environment
Definition TestBase.py:72
print_failed_cases(failed_cases)
Definition TestBase.py:24
print_cmds(cmds)
Definition TestBase.py:30
set_workdir()
Definition TestBase.py:36
print_case_in_file(case_string, out)
Definition TestBase.py:16