0
0

Pytest 実行時にテストケースを記述している同名のファイルがある場合のエラーへの対応方法

Posted at

概要

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_1folder_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_1folder_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 ===============================

image.png

エラーへの対応方法

folder_2配下のファイル名をtest_cases__utilities__2.pyに変更

|--tests
|  |--test_cases
|  |  |--folder_1
|  |  |  |--test_cases__utilities.py
|  |  |--folder_2
|  |  |  |--test_cases__utilities__2.py

テスト再実行後に正常終了することを確認

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0