<@ULVA73B9P> Looking to use Prefect to automate s...
# ask-marvin
m
@Marvin Looking to use Prefect to automate some testing, but the Pytest printouts get kinda mangled (example:
[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
Copy code
@task(
    log_prints=True,
)
def run_pytest(
    target: str, args: str
):
    cmd = f"pytest {target} {args}"
    print(cmd)
    result = ShellOperation(
        commands=[cmd],
    ).run()
m
It seems there are no GitHub issues directly relating to the problems with Pytest printouts when using Prefect's
ShellOperation
, 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:
Copy code
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.
m
K, so, I've got it working! (I wrote to a file then grabbed it with a State Handler passed to
on_failure
and cleaned it with the
AnsiText
library).