0

環境

  • python 3.6.6
  • pytest 4.5.0

背景

pytestでテストコードを書いています。フォルダ構成は以下の通りです。

tests/
 - test_api.py
 - utils_for_test.py

utils_for_test.pyにはテストコードはありません。以下のクラスが存在します。



class TestWrapper:
    """
    テスト用のUtils
    """

    def __init__(self, api):
        self.api = api

質問

以下のコマンドでpytestを実行したら、以下の警告が発生しました。
pipenv run pytest tests -v --cov=src --cov-report=html

=============================================================================================== warnings summary ================================================================================================
tests/utils_for_test.py:23
tests/utils_for_test.py:23
  /home/vagrant/Documents/annofab-api-python-client/tests/utils_for_test.py:23: PytestCollectionWarning: cannot collect test class 'TestWrapper' because it has a __init__ constructor
    class TestWrapper:

-- Docs: https://docs.pytest.org/en/latest/warnings.html

----------- coverage: platform linux, python 3.6.6-final-0 -----------
Coverage HTML written to dir htmlcov

この警告はどのような意味でしょうか?
またどうすれば解決できますか?

私は以下のように考えているため、なぜ「cannot collect test class 'TestWrapper' 」と言われているかが分かりませんでした。

  • utils_for_test.pyはファイル名の先頭にtestが付いていないので、テストコードとして実行されない
  • したがって 'TestWrapper'がテストクラスとして実行されなくて、特に問題ない

参考サイト

https://docs.pytest.org/en/latest/warnings.html#internal-pytest-warnings

1 件の回答 1

1

utils_for_test.py はファイル名の先頭にtestが付いていないので、テストコードとして実行されない

いいえ。
pytestはデフォルトで、 test_*.py*_test.py に一致するファイル名どちらもテスト用モジュールとして読み込みます。
https://docs.pytest.org/en/latest/goodpractices.html#test-discovery

3
  • ファイル名がutils_for_test.pyのままで、クラス名をWrapperForTestという名前に変更したら、解決できました。
    – yuji38kwmt
    2019年5月14日 3:42
  • >test prefixed test functions or methods inside Test prefixed test classes (without an init method)
    – yuji38kwmt
    2019年5月14日 3:42
  • クラス名のprefixも見るんですね。
    – yuji38kwmt
    2019年5月14日 3:42

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。