Using py.test with coverage doesn't include imports

前端 未结 5 1954
离开以前
离开以前 2020-12-14 05:58

For Jedi we want to generate our test coverage. There is a related question in stackoverflow, but it didn\'t help.

We\'re using py.test as a test runner. However, w

相关标签:
5条回答
  • 2020-12-14 06:14

    In my case, all the tests run, but coverage was 0%.

    The fix was:

    $ export PYTHONPATH="."
    

    After the results were correct.

    I had in past few problems with py.test command having problems to import something and setting the PYTHONPATH env var was the solution. It worked for me this time too.

    My real example with awslogs

    First with PYTHONPATH unset:

    $ py.test --cov=awslogs  tests/
    ========================================= test session starts =========================================
    platform linux2 -- Python 2.7.9, pytest-2.8.5, py-1.4.31, pluggy-0.3.1
    rootdir: /home/javl/sandbox/awslogs/github/awslogs, inifile:
    plugins: cov-2.2.0
    collected 11 items
    
    tests/test_it.py ...........Coverage.py warning: No data was collected.
    
    --------------------------- coverage: platform linux2, python 2.7.9-final-0 ---------------------------
    Name                    Stmts   Miss  Cover
    -------------------------------------------
    awslogs/__init__.py         2      2     0%
    awslogs/bin.py             85     85     0%
    awslogs/core.py           143    143     0%
    awslogs/exceptions.py      12     12     0%
    -------------------------------------------
    TOTAL                     242    242     0%
    
    ====================================== 11 passed in 0.38 seconds ======================================
    

    Resulting coverage is 0%.

    Then I set the PYTHONPATH:

    $ export PYTHONPATH="."
    

    and rerun the test:

    $ py.test --cov=awslogs  tests/
    ========================================= test session starts =========================================
    platform linux2 -- Python 2.7.9, pytest-2.8.5, py-1.4.31, pluggy-0.3.1
    rootdir: /home/javl/sandbox/awslogs/github/awslogs, inifile:
    plugins: cov-2.2.0
    collected 11 items
    
    tests/test_it.py ...........
    --------------------------- coverage: platform linux2, python 2.7.9-final-0 ---------------------------
    Name                    Stmts   Miss  Cover
    -------------------------------------------
    awslogs/__init__.py         2      0   100%
    awslogs/bin.py             85      9    89%
    awslogs/core.py           143     12    92%
    awslogs/exceptions.py      12      2    83%
    -------------------------------------------
    TOTAL                     242     23    90%
    
    ====================================== 11 passed in 0.44 seconds ======================================
    

    Now is the coverage 90%.

    WARNING: Manipulating PYTHONPATH can have strange side effects. Currently I run into problem, that pbr based package is creating egg directory when building distributable and if PYTHONPATH is set to ".", it automatically considers the egg related package as installed. For this reason I stopped using pytest-cov and follow the advice to use coverage tool instead.

    0 讨论(0)
  • 2020-12-14 06:21

    @hynekcer gave me the right idea. But basically the easiest solution lies somewhere else:

    Get rid of pytest-cov!

    Use

    coverage run --source jedi -m py.test
    coverage report
    

    instead!!! This way you're just running a coverage on your current py.test configuration, which works perfectly fine! It's also philosophically the right way to go: Make each program do one thing well - py.test runs tests and coverage checks the code coverage.

    Now this might sound like a rant, but really. pytest-cov hasn't been working properly for a while now. Some tests were failing, just because we used it.


    As of 2014, pytest-cov seems to have changed hands. py.test --cov jedi test seems to be a useful command again (look at the comments). However, you don't need to use it. But in combination with xdist it can speed up your coverage reports.

    0 讨论(0)
  • 2020-12-14 06:22

    I had this problem with py.test, the coverage and the django plugin. Apparently the model files are imported before coverage is started. Not even "-p coverage" for early-loading of the coverage-plugin worked.

    I fixed it (ugly?) by removing the models module from sys.modules and re-importing it in the test file that tests the model:

    import sys
    del sys.modules['project.my_app.models']
    from project.my_app import models
    
    def test_my_model():
      ...
    
    0 讨论(0)
  • 2020-12-14 06:25

    if you are using flask then this will help you to resolve the issue-

    pytest --cov=src --cov-report=html
    
    0 讨论(0)
  • 2020-12-14 06:26

    I fixed the test coverage to 94% by this patch that simplifies import dependencies and by the command:

    py.test --cov jedi test                    # or
    py.test --cov jedi test --cov-report=html  # + a listing with red uncovered lines
    

    Uncovered lines are only in conditional commands or in some less used functions but all headers are completely covered.

    The problem was that the tests configuration test/conftest.py did import prematurely by dependencies almost all files in the project. The conftest file defines also additional command line options and settings that should be set before running the test. Therefore I think that pytest_cov plugin works correctly if it ignores everything that was imported together with this file, although it is a pain. I excluded also __init__.py and settings.py from the report because they are simple and with the complete coverage but they are also imported prematurely in the dependency of conftest.

    0 讨论(0)
提交回复
热议问题