63

私は報道に不慣れで、奇妙な問題に遭遇しました。私の記事では、仮想環境のサイト パッケージを考慮しています。カバレッジ実行の出力は次のとおりです。

coverage run test.py
....................
----------------------------------------------------------------------
Ran 20 tests in 0.060s

OK
(atcatalog)- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -jmfrank63@fullstack-audio-text-catalog:~/workspace (git master)       [19:58:45]
$ coverage report
Name                                                                              Stmts   Miss  Cover
-----------------------------------------------------------------------------------------------------
/home/ubuntu/Envs/atcatalog/lib/python2.7/site-packages/flask/__init__               18      0   100%
/home/ubuntu/Envs/atcatalog/lib/python2.7/site-packages/flask/_compat                38     20    47%
/home/ubuntu/Envs/atcatalog/lib/python2.7/site-packages/flask/app                   528    255    52%
/home/ubuntu/Envs/atcatalog/lib/python2.7/site-packages/flask/blueprints            156    118    24%
                             .
                             .
                             .
/home/ubuntu/Envs/atcatalog/lib/python2.7/site-packages/werkzeug/urls               412    215    48%
/home/ubuntu/Envs/atcatalog/lib/python2.7/site-packages/werkzeug/utils              242    175    28%
/home/ubuntu/Envs/atcatalog/lib/python2.7/site-packages/werkzeug/wrappers           568    298    48%
/home/ubuntu/Envs/atcatalog/lib/python2.7/site-packages/werkzeug/wsgi               448    352    21%
atcatalog/__init__                                                                    7      0   100%
atcatalog/views/__init__                                                              0      0   100%
atcatalog/views/publang                                                               7      0   100%
atcatalog/views/pubtext                                                               1      0   100%
atcatalog/views/userlang                                                             13      0   100%
atcatalog/views/users                                                                 5      0   100%
atcatalog/views/usertext                                                             14      0   100%
test                                                                                120      0   100%
-----------------------------------------------------------------------------------------------------
TOTAL                                                                             12530   8044    36%
(atcatalog)- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -jmfrank63@fullstack-audio-text-catalog:~/workspace (git master)       [19:58:55]

ホームの下にある私のプロジェクトディレクトリの構造は次のとおりです。

workspace/
├── README.md
├── atcatalog
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── static
│   ├── templates
│   └── views
│       ├── __init__.py
│       ├── __init__.pyc
│       ├── publang.py
│       ├── publang.pyc
│       ├── pubtext.py
│       ├── pubtext.pyc
│       ├── userlang.py
│       ├── userlang.pyc
│       ├── users.py
│       ├── users.pyc
│       ├── usertext.py
│       └── usertext.pyc
├── requirements.txt
├── run.py
└── test.py

最初はプロジェクト ディレクトリ内に仮想環境があり、現在は virtualenvwrapper を使用して ~/Envs に移動しましたが、問題は解決しませんでした。run.py と test.py は決して特別なものではなく、どちらも atcatalog からアプリをインポートします。また、仮想環境ディレクトリを省略する方法を見つけようとしましたが、Google からは (驚くべきことに) 回答が得られませんでした。十分にテストされたサイト パッケージをテストすることは、カバレッジの目的ではないと思います。したがって、私はそれらを実行から除外します。

サイト パッケージをテストしてカバレッジを回避するにはどうすればよいですか?

4

4 に答える 4

79

tknickmanのおかげで私はそれを理解しました:どちらかを使用してください

coverage run --source <path to project dir> test.py

または、次の内容で、カバレッジを実行するディレクトリにある構成ファイル .coveragerc を作成します。

[run]
source =
    <path to project dir>

これは、仮想環境がプロジェクト ディレクトリにインストールされていないことを示しています。プロジェクトディレクトリの下に仮想環境がインストールされている場合は、使用できます

coverage run --source <project path> --omit <pattern> test.py

omit は次のようなファイル パターンを必要とすることに注意してください。

~/projectdir/venv/*

パスの代わりに。

対応する .coveragerc は次のようになります。

[run]
source=
    <path to project dir>
omit=
    <path to project dir>/<name of virtual env>/*

標準ライブラリのパッケージのように、site-packages の下にインストールされたパッケージはデフォルトでカバーされるべきではないと今でも思います。

于 2015-08-23T12:01:34.490 に答える
7

py.testを使用してから、 setup.cfgファイルでテスト オプションを指定してみてください。最初に pip install pytest を実行する必要があります。

例えば:

[pytest]
norecursedirs = build docs/_build *.egg .tox *.venv
python_files = tests/functional* tests/integration*
addopts =
    #--verbose
    --tb short
    # Turn on --capture to have brief, less noisy output
    # You will only see output if the test fails
    # Use --capture no if you want to see it all or have problems debugging
    --capture fd
    # --capture no
    # show extra test summary info as specified by chars (f)ailed, (E)error,      (s)skipped, (x)failed, (X)passed.
    - rfEsxX
    --junitxml junit.xml
    --cov workspace --cov-report xml --cov-report term-missing

py.test の構成について詳しくは、https ://pytest.org/latest/customize.html をご覧ください。

于 2015-08-22T20:44:01.573 に答える
1

pytest を使用している場合は、テストする専用パスまたはファイルを指定できますsetup.cfg( docs を参照)。

[pytest]
# a directory
testpaths = tests

# exact file(s)
python_files = tests/test1.py tests/test2.py

python_filestestpathsパラメータを含めると、python_filesのみが使用されるようです。

于 2016-06-29T05:42:07.977 に答える