概要
Pytest 実行時にテストケースを記述しているファイルが同名の場合に、次のようなエラーが発生する際の対応方法を紹介します。基本的な対応方法としては、同時に実行する必要があるテストケースのファイル名が被らないようにすることです。
collecting ... collected 2 items / 1 error
==================================== ERRORS ====================================
_ ERROR collecting pytest_test/tests/test_cases/folder_2/test_cases__utilities.py _
import file mismatch:
imported module 'test_cases__utilities' has this __file__ attribute:
/Workspace/Repos/Shared/databricks_tecks_fro_qiita_2/pytest_test/tests/test_cases/folder_1/test_cases__utilities.py
which is not the same as the test file we want to collect:
/Workspace/Repos/Shared/databricks_tecks_fro_qiita_2/pytest_test/tests/test_cases/folder_2/test_cases__utilities.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 2.86s ===============================
テストケースを記述しているファイルが同名である場合とは、下記のようなディレクトリとなっているような場合です。folder_1
とfolder_2
の配下に、test_cases__utilities.py
という同名のファイルがあります。このような構成の場合には、Pytest ではエラーとなってしまうようです。
|--tests
| |--test_cases
| | |--folder_1
| | | |--test_cases__utilities.py
| | |--folder_2
| | | |--test_cases__utilities.py
エラーの再現方法と対応法を共有します。
Databricks 上でのエラーの再現方法
テスト対象のコードとテストケースを準備
以前に投稿した下記記事のプログラムを Databricks Repos 上に容易します。
テストケースを記述しているtest_cases__utilities.py
を下記のコードに書き換えます。テストケースでエラーを発生させないためです。
import pytest
from pytest_test.src.utilities import add_num
def test__add_num__001():
"""successes(正常系テスト)"""
assert add_num(1, 1) == 2
def test__add_num__002():
"""successes(異常系テスト)"""
# 文字を引数とすることでエラーとなる想定
with pytest.raises(TypeError) as e:
add_num(1, 'ABC')
pytest_test/tests/test_cases
ディレクトリ配下に、folder_1
とfolder_2
というフォルダーを作成してtest_cases__utilities.py
をそれぞれのディレクトリに配置します。Databricks ではファイルを Clone することができないため、ファイルを Export & Import することでコピーできます。
|--tests
| |--test_cases
| | |--folder_1
| | | |--test_cases__utilities.py
| | |--folder_2
| | | |--test_cases__utilities.py
pytest_test/tests/test_runner
の実行によりテストがエラーとなることを確認
collecting ... collected 2 items / 1 error
==================================== ERRORS ====================================
_ ERROR collecting pytest_test/tests/test_cases/folder_2/test_cases__utilities.py _
import file mismatch:
imported module 'test_cases__utilities' has this __file__ attribute:
/Workspace/Repos/Shared/databricks_tecks_fro_qiita_2/pytest_test/tests/test_cases/folder_1/test_cases__utilities.py
which is not the same as the test file we want to collect:
/Workspace/Repos/Shared/databricks_tecks_fro_qiita_2/pytest_test/tests/test_cases/folder_2/test_cases__utilities.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 2.86s ===============================
エラーへの対応方法
folder_2
配下のファイル名をtest_cases__utilities__2.py
に変更
|--tests
| |--test_cases
| | |--folder_1
| | | |--test_cases__utilities.py
| | |--folder_2
| | | |--test_cases__utilities__2.py