Matt Alhonte
01/24/2024, 11:44 PM[37m@given[39;49;00m([90m[39;49;00m
). Is there a good way to do it? Currently I'm using prefect_shell
to run Pytest
@task(
log_prints=True,
)
def run_pytest(
target: str, args: str
):
cmd = f"pytest {target} {args}"
print(cmd)
result = ShellOperation(
commands=[cmd],
).run()
Marvin
01/24/2024, 11:45 PMShellOperation
, but here are some steps you can try based on best practices and documentation:
1. Disable colored output in Pytest. You can do this by adding the -p no:cacheprovider
option to your Pytest command, which should prevent it from emitting ANSI escape sequences that can cause the mangled output.
2. Use the --color=no
flag with your Pytest command to explicitly turn off colored output.
3. Prefect context manager for logs can be used to set the logging level that may help suppress unnecessary output.
4. Redirect Pytest outputs to a file and then read it back, which can be cleaner. Here's a modification to your existing implementation:
python
@task(
on_failure=[print_error],
log_prints=True,
)
def run_pytest(target: str, args: str):
cmd = f"pytest {target} {args}"
# Specify --color=no to prevent colored output
cmd_no_color = cmd + " --color=no"
print(cmd_no_color)
result = ShellOperation(
commands=[cmd_no_color],
).run()
5. Consider picking the output format that is most suitable for your needs. Pytest supports multiple output formats including plain, simple, detailed, etc., which you can specify with the -r
option followed by the format identifier.
You can try these adjustments to make the Pytest printouts more readable within the context of your Prefect tasks. Remember to test these changes locally before deploying them to your Prefect flows.Matt Alhonte
01/25/2024, 2:52 AMon_failure
and cleaned it with the AnsiText
library).