Skip to content

Commit

Permalink
PR IntelRealSense#7836 from Matan: run-unit-tests.py ability to run t…
Browse files Browse the repository at this point in the history
…ests by name
  • Loading branch information
maloel authored Nov 30, 2020
2 parents a98483f + 157d9a7 commit 0d3d3ba
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 66 deletions.
10 changes: 5 additions & 5 deletions src/l500/ac-trigger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,8 @@ namespace ivcam2 {
if( stdOut != NULL && stdOut != INVALID_HANDLE_VALUE )
{
DWORD written = 0;
WriteConsoleA( stdOut, s.c_str(), (DWORD)s.length(), &written, NULL );
WriteConsoleA( stdOut, "\n\r", 2, &written, NULL );
WriteFile( stdOut, s.c_str(), (DWORD)s.length(), &written, NULL );
WriteFile( stdOut, "\n", 1, &written, NULL );
}
#else
std::cout << s << std::endl;
Expand Down Expand Up @@ -1271,10 +1271,10 @@ namespace ivcam2 {
}
catch( std::exception const & e )
{
AC_LOG( DEBUG,
std::string( to_string() << "Error while checking alternate IR option: " << e.what() ) );
AC_LOG( DEBUG,
std::string( to_string() << "Error while checking alternate IR option: " << e.what() ) );
}
if (alt_ir_is_on)
if( alt_ir_is_on )
{
if( ! invalid_reason.empty() )
invalid_reason += ", ";
Expand Down
4 changes: 3 additions & 1 deletion unit-tests/algo/d2rgb/test-bad-conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
#############################################################################################
test.start("Failing check_conditions function")
# If ambient light is RS2_DIGITAL_GAIN_LOW (2) receiver_gain must be 18
depth_sensor.set_option(rs.option.ambient_light, 2)
# If ambient light is RS2_DIGITAL_GAIN_HIGH (1) receiver_gain must be 9
old_receiver_gain = depth_sensor.get_option(rs.option.receiver_gain)
depth_sensor.set_option(rs.option.receiver_gain, 15)
try:
d2r.trigger_device_calibration( rs.calibration_type.manual_depth_to_rgb )
Expand All @@ -46,6 +47,7 @@
else:
test.unexpected_exception()
test.check_equal_lists(ac.status_list, [rs.calibration_status.bad_conditions])
depth_sensor.set_option(rs.option.receiver_gain, old_receiver_gain)
test.finish()
#############################################################################################
test.print_results_and_exit()
6 changes: 2 additions & 4 deletions unit-tests/algo/d2rgb/test-frame-drop.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import pyrealsense2 as rs, test, ac


# We set the environment variables to suit this test
test.set_env_vars({"RS2_AC_DISABLE_CONDITIONS":"1",
"RS2_AC_DISABLE_RETRIES":"1",
"RS2_AC_FORCE_BAD_RESULT":"1",
Expand All @@ -16,10 +14,8 @@

# Resetting sensors to factory calibration
dcs = rs.calibrated_sensor(depth_sensor)
dcs.reset_calibration()

ccs = rs.calibrated_sensor(color_sensor)
ccs.reset_calibration()

d2r = rs.device_calibration(dev)
d2r.register_calibration_change_callback( ac.status_list_callback )
Expand Down Expand Up @@ -82,6 +78,8 @@ def depth_frame_call_back(frame):
test.start("Checking for frame drops in", n_cal, "calibrations")
for i in range(n_cal):
try:
dcs.reset_calibration()
ccs.reset_calibration()
d2r.trigger_device_calibration( rs.calibration_type.manual_depth_to_rgb )
ac.wait_for_calibration()
except:
Expand Down
4 changes: 4 additions & 0 deletions unit-tests/algo/d2rgb/test-triggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
# Test #3
test.start("Color sensor is on")
ac.reset_status_list()
dcs.reset_calibration()
ccs.reset_calibration()
color_sensor.open( cp )
color_sensor.start( lambda f: None )
try:
Expand All @@ -108,6 +110,8 @@
# Test #4
test.start("2 AC triggers in a row")
ac.reset_status_list()
dcs.reset_calibration()
ccs.reset_calibration()
color_sensor.start( lambda f: None )
try:
d2r.trigger_device_calibration( rs.calibration_type.manual_depth_to_rgb )
Expand Down
13 changes: 8 additions & 5 deletions unit-tests/py/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ def set_env_vars(env_vars):
os.environ[env_var] = val
sys.argv.append("rerun")
if platform.system() == 'Linux' and "microsoft" not in platform.uname()[3].lower():
cmd = ["python3"] + sys.argv
cmd = ["python3"]
else:
cmd = ["py", "-3"] + sys.argv
cmd = ["py", "-3"]
if sys.flags.verbose:
cmd += ["-v"]
cmd += sys.argv
p = subprocess.run( cmd, stderr=subprocess.PIPE, universal_newlines=True )
exit(p.returncode)

Expand Down Expand Up @@ -154,14 +157,14 @@ def fail():
test_failed = True

# Functions for formatting test cases
def start(test_name):
def start(*test_name):
global n_tests, test_failed, test_in_progress
if test_in_progress:
raise RuntimeError("Tried to start test before previous test finished. Aborting test")
n_tests += 1
test_failed = False
test_in_progress = True
print(test_name)
print(*test_name)

def finish():
global test_failed, n_failed_tests, test_in_progress
Expand All @@ -178,7 +181,7 @@ def finish():
# The format has to agree with the expected format in check_log() in run-unit-tests and with the C++ format using Catch
def print_results_and_exit():
global n_assertions, n_tests, n_failed_assertions, n_failed_tests
if n_failed_assertions:
if n_failed_tests:
passed = n_assertions - n_failed_assertions
print("test cases:", n_tests, "|" , n_failed_tests, "failed")
print("assertions:", n_assertions, "|", passed, "passed |", n_failed_assertions, "failed")
Expand Down
144 changes: 93 additions & 51 deletions unit-tests/run-unit-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

def usage():
ourname = os.path.basename(sys.argv[0])
print( 'Syntax: ' + ourname + ' <dir>' )
print( ' Run all unit-tests in $dir (in Windows: .../build/Release; in Linux: .../build' )
print( 'Syntax: ' + ourname + ' <dir-or-regex>' )
print( ' If given a directory, run all unit-tests in $dir (in Windows: .../build/Release; in Linux: .../build' )
print( ' Test logs are kept in <dir>/unit-tests/<test-name>.log' )
print( ' If given a regular expression, run all python tests in unit-tests directory that fit <regex>, to stdout' )
print( 'Options:' )
print( ' --debug Turn on debugging information' )
print( ' -v, --verbose Errors will dump the log to stdout' )
Expand All @@ -19,8 +20,21 @@ def debug(*args):
pass


def stream_has_color( stream ):
if not hasattr(stream, "isatty"):
return False
if not stream.isatty():
return False # auto color only on TTYs
try:
import curses
curses.setupterm()
return curses.tigetnum( "colors" ) > 2
except:
# guess false in case of error
return False

# Set up the default output system; if not a terminal, disable colors!
if sys.stdout.isatty():
if stream_has_color( sys.stdout ):
red = '\033[91m'
gray = '\033[90m'
reset = '\033[0m'
Expand All @@ -38,11 +52,11 @@ def progress(*args):
global _progress
_progress = args
else:
red = reset = cr = clear_eos = ''
red = gray = reset = cr = clear_eos = ''
def out(*args):
print( *args )
def progress(*args):
debug( *args )
print( *args )

n_errors = 0
def error(*args):
Expand Down Expand Up @@ -78,17 +92,12 @@ def out(*args):
pass
if len(args) != 1:
usage()
dir = args[0]
if not os.path.isdir( dir ):
error( 'Directory not found:', dir )
sys.exit(1)


def run(cmd, stdout=subprocess.PIPE):
def run( cmd, stdout = None ):
debug( 'Running:', cmd )
handle = None
try:
if stdout != subprocess.PIPE:
if stdout and stdout != subprocess.PIPE:
handle = open( stdout, "w" )
stdout = handle
rv = subprocess.run( cmd,
Expand Down Expand Up @@ -120,6 +129,71 @@ def find( dir, mask ):
debug(leaf)
yield leaf

# NOTE: WSL will read as 'Linux' but the build is Windows-based!
system = platform.system()
if system == 'Linux' and "microsoft" not in platform.uname()[3].lower():
linux = True
else:
linux = False

current_dir = os.path.dirname(os.path.abspath(__file__))
# this script is located in librealsense/unit-tests, so one directory up is the main repository
librealsense = os.path.dirname(current_dir)

# Python scripts should be able to find the pyrealsense2 .pyd or else they won't work. We don't know
# if the user (Travis included) has pyrealsense2 installed but even if so, we want to use the one we compiled.
# we search the librealsense repository for the .pyd file (.so file in linux)
pyrs = ""
if linux:
for so in find(librealsense, '(^|/)pyrealsense2.*\.so$'):
pyrs = so
else:
for pyd in find(librealsense, '(^|/)pyrealsense2.*\.pyd$'):
pyrs = pyd

if pyrs:
# After use of find, pyrs contains the path from librealsense to the pyrealsense that was found
# We append it to the librealsense path to get an absolute path to the file to add to PYTHONPATH so it can be found by the tests
pyrs_path = librealsense + os.sep + pyrs
# We need to add the directory not the file itself
pyrs_path = os.path.dirname(pyrs_path)
# Add the necessary path to the PYTHONPATH environment variable so python will look for modules there
os.environ["PYTHONPATH"] = pyrs_path
# We also need to add the path to the python packages that the tests use
os.environ["PYTHONPATH"] += os.pathsep + (current_dir + os.sep + "py")
# We can simply change `sys.path` but any child python scripts won't see it. We change the environment instead.

target = args[0]

# If a regular expression (not a directory) is passed in, find the test(s) and run
# them directly
if not os.path.isdir( target ):
if not pyrs:
error( "Python wrappers (pyrealsense2*." + pyrs + ") not found" )
usage()
n_tests = 0
for py_test in find(current_dir, target):
n_tests += 1
progress( py_test + ' ...' )
if linux:
cmd = ["python3"]
else:
cmd = ["py", "-3"]
if sys.flags.verbose:
cmd += ["-v"]
cmd += [current_dir + os.sep + py_test]
try:
run( cmd )
except subprocess.CalledProcessError as cpe:
error( cpe )
error( red + py_test + reset + ': exited with non-zero value! (' + str(cpe.returncode) + ')' )
if n_errors:
sys.exit(1)
if not n_tests:
error( "No tests found matching: " + target )
usage()
sys.exit(0)

def remove_newlines (lines):
for line in lines:
if line[-1] == '\n':
Expand Down Expand Up @@ -190,24 +264,17 @@ def check_log_for_fails(log, testname, exe):
return True
return False

logdir = dir + '/unit-tests'
os.makedirs( logdir, exist_ok = True );
logdir = target + '/unit-tests'
os.makedirs( logdir, exist_ok = True )
n_tests = 0

# In Linux, the build targets are located elsewhere than on Windows
# NOTE: WSL will read as 'Linux' but the build is Windows-based!
system = platform.system()
if system == 'Linux' and "microsoft" not in platform.uname()[3].lower():
linux = True
else:
linux = False

# Go over all the tests from a "manifest" we take from the result of the last CMake
# run (rather than, for example, looking for test-* in the build-directory):
if linux:
manifestfile = dir + '/CMakeFiles/TargetDirectories.txt'
manifestfile = target + '/CMakeFiles/TargetDirectories.txt'
else:
manifestfile = dir + '/../CMakeFiles/TargetDirectories.txt'
manifestfile = target + '/../CMakeFiles/TargetDirectories.txt'

for manifest_ctx in grep( r'(?<=unit-tests/build/)\S+(?=/CMakeFiles/test-\S+.dir$)', manifestfile ):

Expand All @@ -218,9 +285,9 @@ def check_log_for_fails(log, testname, exe):
else:
testname = testdir # no parent folder so we get "test-all"
if linux:
exe = dir + '/unit-tests/build/' + testdir + '/' + testname
exe = target + '/unit-tests/build/' + testdir + '/' + testname
else:
exe = dir + '/' + testname + '.exe'
exe = target + '/' + testname + '.exe'
log = logdir + '/' + testname + '.log'

progress( testname, '>', log, '...' )
Expand All @@ -235,33 +302,8 @@ def check_log_for_fails(log, testname, exe):
# An unexpected error occurred
error( red + testname + reset + ': exited with non-zero value! (' + str(cpe.returncode) + ')' )

current_dir = os.path.dirname(os.path.abspath(__file__))
# this script is located in librealsense/unit-tests, so one directory up is the main repository
librealsense = os.path.dirname(current_dir)

# Python scripts should be able to find the pyrealsense2 .pyd or else they won't work. We don't know
# if the user (Travis included) has pyrealsense2 installed but even if so, we want to use the one we compiled.
# we search the librealsense repository for the .pyd file (.so file in linux)
pyrs = ""
if linux:
for so in find(librealsense, '(^|/)pyrealsense2.*\.so$'):
pyrs = so
else:
for pyd in find(librealsense, '(^|/)pyrealsense2.*\.pyd$'):
pyrs = pyd
# if we run python tests with no .pyd/.so file they will crash. Therefore we only run them if such a file was found
# If we run python tests with no .pyd/.so file they will crash. Therefore we only run them if such a file was found
if pyrs:
# After use of find, pyrs contains the path from librealsense to the pyrealsense that was found
# We append it to the librealsense path to get an absolute path to the file to add to PYTHONPATH so it can be found by the tests
pyrs_path = librealsense + os.sep + pyrs
# We need to add the directory not the file itself
pyrs_path = os.path.dirname(pyrs_path)
# Add the necessary path to the PYTHONPATH environment variable so python will look for modules there
os.environ["PYTHONPATH"] = pyrs_path
# We also need to add the path to the python packages that the tests use
os.environ["PYTHONPATH"] += os.pathsep + (current_dir + os.sep + "py")
# We can simply change `sys.path` but any child python scripts won't see it. We change the environment instead.

# unit-test scripts are in the same directory as this script
for py_test in find(current_dir, '(^|/)test-.*\.py'):

Expand Down

0 comments on commit 0d3d3ba

Please sign in to comment.