question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Coverage report generation incompatible with pytest `tmpdir` feature

See original GitHub issue

Given a project structure like:

myproject
├── myproject
│   └── __init__.py
├── tests
│   └── myproject
│       └── test.py
└── setup.cfg

I’m running the command in the toplevel myproject directory:

py.test --cov=myproject --cov-report html

On stdout I can see the log output:

Coverage HTML written to dir htmlcov

However no output has been written, i.e., the myproject directory does not contain htmlcov (and neither of its subdirectories). Even a find / -name "*htmlcov*" reveals nothing.

Notes:

  • The output is written if I use --cov-report html:/absolute/path/to/myproject/htmlcov.
  • I get the same issue for --cov-report xml.
  • My tests chdir a lot – can this mess up pytest-cov?

Versions:

  • pytest: 3.8.1
  • pytest-cov: 2.6.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:16 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
tbaledentcommented, Jan 14, 2020

I also ran into the same issue recently where I couldn’t chdir into pytest’s tmpdir feature without breaking how pytest-cov locates the coverage report at the end of a test suite, so I figured I would leave a comment with a quick solution to help anyone else who might have the same problems in the future. To keep the line tmpdir.chdir() in the initdir fixture without adversely affecting any downstream tests or the coverage report generation, simply add a constant at the top of your test like OLD_DIR = os.getcwd() and then add the following to your tearDownClass (assuming you’re using the pytest fixtures within a unittest TestCase):

    @classmethod
    def tearDownClass(cls) -> None:
        # tear down some other stuff
        os.chdir(OLD_DIR)

This will cleanly revert the current working directory to the previous directory before tmpdir once the tests have completed so the coverage report can successfully be located after the test suite has completed.

1reaction
ionelmccommented, Nov 16, 2018

We could try to make sure we dump the report in the original cwd tho (just like we handle config paths).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Full pytest documentation
How to write and report assertions in tests · Asserting with the assert statement · Assertions about expected exceptions · Assertions about expected ......
Read more >
pytest Documentation - Read the Docs
Execute the test function with “quiet” reporting mode: $ pytest -q ... pytest-cov: coverage reporting, compatible with distributed testing.
Read more >
Release 5.3.1 unknown - Coverage.py
Coverage.py is a tool for measuring code coverage of Python programs. It monitors your program, noting which parts.
Read more >
Diff - refs/heads/experimental/pyarmnn^! - ml/armnn - Gitiles
+enable= + + +[REPORTS] + +# Python expression which should return a note less than ... is used to generate the Arm NN...
Read more >
Changelog — Dask.distributed 2022.12.1 documentation
Work around incompatibility of crick with setuptools 65 (GH#6887) crusaderky ... Generate junit report when pytest-timeout kills pytest (GH#5832) crusaderky.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found