Search code examples
pythontensorflowpytestcoverage.pypytest-cov

Disabling tf.function decorators for code coverage pytest run


As discussed here, code coverage tools do not work nicely with tensorflow due to its code transformation. One suggested workaround is to use tf.config.experimental_run_functions_eagerly(True) when generating reports (though it's worth noting that this still does not handle all cases, e.g. tf.map_fn).

My question is: is there a simple way to do this automatically for tests run using pytest --cov? Is there perhaps something I could add to conftest.py that would allow me to make all executions run eagerly whenever I pass a given command line argument, such as pytest --cov --eagerly?


Solution

  • Is there perhaps something I could add to conftest.py that would allow me to make all executions run eagerly whenever I pass a given command line argument,

    Yes, it turns out:

    def pytest_addoption(parser: Parser) -> None:
        parser.addoption(
            "--eager",
            action="store_true",
            default=False,
            help="whether to run all functions eagerly",
        )
        
    def pytest_collection_modifyitems(config: Config, items: list[pytest.Item]) -> None:
        if config.getoption("--eager"):
            import tensorflow as tf
            tf.config.experimental_run_functions_eagerly(True)