LoginSignup
1
1

Databricks にて pytest によるテスト対象のディレクトリにノートブックが含まれる場合のエラーについて

Posted at

概要

Databricks にて pytest 実行時にテスト対象のディレクトリにノートブックが含まれている場合には、次のようなエラーが発生するようです。そのエラーに対しては、ノートブックを削除して、python ファイルを新規で作成してテストコードを保持させることでエラーが解消されます。エラーの再現方法と対応方法を共有します。

============================= test session starts ==============================
platform linux -- Python 3.10.12, pytest-8.1.1, pluggy-1.4.0 -- /local_disk0/.ephemeral_nfs/envs/pythonEnv-4ec1a9d6-6be6-4f9a-9eba-0f2b70be94c9/bin/python
cachedir: .pytest_cache
rootdir: /Workspace/Repos/Shared/databricks_tecks_fro_qiita_2
collecting ... collected 0 items / 1 error

==================================== ERRORS ====================================
ERROR collecting pytest_test/tests/test_cases/test_cases__utilities.py ____

E OSError: [Errno 95] Operation not supported: '/Workspace/Repos/Shared/databricks_tecks_fro_qiita_2/pytest_test/tests/test_cases/test_cases__utilities.py'
buffering = -1
encoding = None
errors = None
mode = 'rb'
newline = None
self = PosixPath('/Workspace/Repos/Shared/databricks_tecks_fro_qiita_2/pytest_test/tests/test_cases/test_cases__utilities.py')
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 1.98s ===============================

指定したディレクトリを確認するとファイルらしきものがあり気づくことが困難ではありますが、テストケースのファイルのロゴがノートブックとなっていることで気づくことができます。

image.png

image.png

Databricks 上で pytest によりテストを実行する方法については次の記事で紹介しています。

エラーの再現方法

Databricks Repos 上に次の URL から Repo を作成します。

image.png

pytest_test/tests/test_cases/test_cases__utilities.pyパスにあるファイルを削除します。

image.png

pytest_test/tests/test_cases/test_cases__utilities.pyディレクトリに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')

image.png

テストを実行するとエラー終了となることを確認

image.png

対応方法

ノートブックを削除

image.png

pytest_test/tests/test_cases/test_cases__utilities.pyディレクトリに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')

image.png

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

image.png

1
1
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
1
1