""" . format(tr_style=tr_style,
test_category=test_category,
result_attr=result_attr,
name=name,
testname=test_result.name,
status=status,
new_url=new_url,
ref_url=ref_url)
self.compare_tests += test_html
def _get_render_arguments(
self,
arguments_cb: ArgumentsCallback,
filepath: Path,
base_output_filepath: Path,
) -> list[str | Path]:
# Each render test can override this method to provide extra functionality.
# See Cycles render tests for an example.
# Do not delete.
return arguments_cb(filepath, base_output_filepath)
def _get_arguments_suffix(self) -> list[str]:
# Get command line arguments that need to be provided after all file-specific ones.
# For example the Cycles render device argument needs to be added at the end of
# the argument list, otherwise tests can't be batched together.
#
# Each render test is supposed to override this method.
return []
def _get_filepath_tests(self, filepath: Path) -> list[TestResult]:
list_filepath = filepath.with_name(filepath.stem + "_permutations.txt")
if list_filepath.exists():
with open(list_filepath, 'r') as file:
return [TestResult(self, filepath, testname.rstrip('\n')) for testname in file]
else:
testname = test_get_name(filepath)
return [TestResult(self, filepath, testname)]
def _run_tests(
self,
filepaths: list[Path],
blender: Path,
arguments_cb: ArgumentsCallback,
batch: bool,
) -> list[TestResult]:
# Run multiple tests in a single Blender process since startup can be
# a significant factor. In case of crashes, re-run the remaining tests.
verbose = os.environ.get("BLENDER_VERBOSE") is not None
remaining_filepaths = filepaths[:]
test_results = []
arguments_suffix = self._get_arguments_suffix()
while len(remaining_filepaths) > 0:
command: list[str | Path] = [blender]
running_tests = []
# On Windows, there is a maximum length of 32,767 characters (including the terminating null character)
# for process command line commands, see:
# https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa
command_line_length = len(str(blender))
for suffix in arguments_suffix:
# Add 3 for taking into account spaces and quotation marks potentially added by Python.
command_line_length += len(str(suffix)) + 3
# Construct output filepaths and command to run
for filepath in remaining_filepaths:
testname = test_get_name(filepath)
base_output_filepath = self.output_dir / ("tmp_" + testname)
command_filepath = self._get_render_arguments(arguments_cb, filepath, base_output_filepath)
# Check if we have surpassed the command line limit.
for cmd in command_filepath:
command_line_length += len(str(cmd)) + 3
if sys.platform == 'win32' and command_line_length > 32766 and len(running_tests) > 0:
break
print_message(testname, 'SUCCESS', 'RUN')
running_tests.append(filepath)
command.extend(command_filepath)
output_filepath = self.output_dir / ("tmp_" + testname + "0001.png")
output_filepath.unlink(missing_ok=True)
# Only chain multiple commands for batch
if not batch:
break
command.extend(arguments_suffix)
# Run process
crash = False
output = None
try:
completed_process = subprocess.run(command, stdout=subprocess.PIPE)
if completed_process.returncode != 0:
crash = True
output = completed_process.stdout
except Exception:
crash = True
if verbose:
def quote_expr_args(cmd: list[str | Path]) -> list[str]:
quoted = []
quote_next = False
for arg in cmd:
if quote_next:
quoted.append('"{}"'.format(arg)) # wrap the expression in quotes
quote_next = False
else:
quoted.append(str(arg))
if arg == "--python-expr":
quote_next = True
return quoted
print(' '.join(quote_expr_args(command)))
if (verbose or crash) and output:
print(output.decode("utf-8", 'ignore'))
tests_to_check = []
# Detect missing filepaths and consider those errors
for filepath in running_tests:
remaining_filepaths.pop(0)
file_crashed = False
for test in self._get_filepath_tests(filepath):
self.postprocess_test(blender, test)
if not test.tmp_out_img.exists() or test.tmp_out_img.stat().st_size == 0:
if crash:
# In case of crash, stop after missing files and re-render remaining
test.error = "CRASH"
test_results.append(test)
file_crashed = True
break
else:
test.error = "NO OUTPUT"
test_results.append(test)
else:
tests_to_check.append(test)
if file_crashed:
break
pool = multiprocessing.Pool(multiprocessing.cpu_count())
test_results.extend(pool.starmap(diff_output,
[(test, self.oiiotool, self.fail_threshold, self.fail_percent, self.verbose, self.update)
for test in tests_to_check]))
pool.close()
for test in test_results:
if test.error == "CRASH":
print_message("Crash running Blender")
print_message(test.name, 'FAILURE', 'FAILED')
elif test.error == "NO OUTPUT":
print_message("No render result file found")
print_message(str(test.tmp_out_img), 'FAILURE', 'FAILED')
elif test.error == "VERIFY":
print_message("Render result is different from reference image")
print_message(test.name, 'FAILURE', 'FAILED')
else:
print_message(test.name, 'SUCCESS', 'OK')
test.tmp_out_img.unlink(missing_ok=True)
return test_results
def postprocess_test(self, blender: Path, test: TestResult) -> None:
"""
Post-process test result after the Blender has run.
For example, this function is where conversion from video to a still image suitable for image diffing.
"""
pass
def _run_all_tests(
self,
dirname: str,
dirpath: Path,
blender: Path,
arguments_cb: ArgumentsCallback,
batch: bool,
fail_silently: bool,
) -> bool:
if self.filter:
print_message(f"Note: Blender Test filter = {self.filter}", type='WARNING', status="RAW")
passed_tests = []
failed_tests = []
silently_failed_tests = []
all_files = list(blend_list(dirpath, self.blocklist, self.filter))
all_files.sort()
if not list(blend_list(dirpath, [], "")):
print_message("No .blend files found in '{}'!".format(dirpath), 'FAILURE', 'FAILED')
return False
print_message("Running {} tests from 1 test case." .
format(len(all_files)),
'SUCCESS', "==========")
time_start = time.time()
test_results = self._run_tests(all_files, blender, arguments_cb, batch)
for test in test_results:
if test.error:
if test.error == "NO_ENGINE":
return False
elif test.error == "NO_START":
return False
if fail_silently and test.error != 'CRASH':
silently_failed_tests.append(test.name)
else:
failed_tests.append(test.name)
else:
passed_tests.append(test.name)
self._write_test_html(dirname, test)
time_end = time.time()
elapsed_ms = int((time_end - time_start) * 1000)
print_message("")
print_message("{} tests from 1 test case ran. ({} ms total)" .
format(len(all_files), elapsed_ms),
'SUCCESS', "==========")
print_message("{} tests." .
format(len(passed_tests)),
'SUCCESS', 'PASSED')
all_failed_tests = silently_failed_tests + failed_tests
if all_failed_tests:
print_message("{} tests, listed below:" .
format(len(all_failed_tests)),
'FAILURE', 'FAILED')
all_failed_tests.sort()
for test in all_failed_tests:
print_message("{}" . format(test), 'FAILURE', "FAILED")
return not bool(failed_tests)