2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Databricks Workspace 上で pytest によるテストケース作成とテスト実行を行う方法

Last updated at Posted at 2023-05-15

概要

Databricks Workspace ファイル機能を利用することで、Databricks Workspace 上で pytest によるテストケース作成とテスト実行が可能であることが判明したため、検証結果を共有します。pytest をベースに記述した py 形式のファイルのテストケースを pytest で実行できるようになり、実行結果としては次のようになりました。

image.png

コードは次のリンク先に確認できます。

検証の準備

ディレクトリ構成

|--README.md
|--src
|  |--utilities.py
|--tests
|  |--test_cases
|  |  |--test_cases__utilities.py
|  |--test_runner <-- テストを実行するノートブック

src/utilities.py

# 2つの引数を加算する関数を定義
def add_num(a, b):
    return a + b

tests/test_cases/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')

def test__add_num__003():
    """failures"""
    assert add_num(1, 1) == 1

def test__add_num__004():
    """failures(errors)"""
    assert add_num(1 + "a") ==1

@pytest.mark.skip(reason='スキップ用')
def test__add_num__005():
    """skipped"""
    assert a

@pytest.mark.xfail
def test__add_num__006():
    """XFAIL"""
    assert add_num(1, 1) == 1

@pytest.mark.xfail
def test__add_num__007():
    """XPASS"""
    assert add_num(1, 1) == 2

実行検証

1. pytest のインストール

# install pytest
%pip install pytest -q

image.png

2. カレントディレクトリを Repo のルートに設定

# カレントディレクトリを Repo のルートに設定
import os
 
notebook_path = dbutils.notebook.entry_point.getDbutils().notebook().getContext().notebookPath().get()
repo_root = os.path.dirname(os.path.dirname(notebook_path))
repo_dir = f'/Workspace{repo_root}'
os.chdir(repo_dir)
os.chdir('../')
print(os.getcwd())

image.png

3. pycache を作成しないように設定

# __pycache__ を作成しないように設定
import sys
sys.dont_write_bytecode = True

image.png

4. ディレクトリを指定

# テストケースを保持しているディレクトリを指定
test_path = 'pytest_test/tests/test_cases'

image.png

5. テストを実行

エラーとなるテストケースが含まれているため、ノートブックの実行がエラーとなります。

# テストを実行
import pytest
pytest_pre_args = []
pytest_discovery_args = [
    "-v",
    "-rsx",
    "--showlocals",
    "--tb=short",
    "-s",
    "--pyargs",
    test_path,
]
pytest_pre_args.extend(pytest_discovery_args)
ut_result = pytest.main(pytest_pre_args)
 
assert ut_result == 0, "The test is failed."
============================= test session starts ==============================
platform linux -- Python 3.9.5, pytest-7.3.1, pluggy-1.0.0 -- /local_disk0/.ephemeral_nfs/envs/pythonEnv-30674075-e389-498e-8621-1c5d9acba497/bin/python
cachedir: .pytest_cache
rootdir: /Workspace/Repos/Shared/databricks_tecks_fro_qiita_2
collecting ... collected 7 items

pytest_test/tests/test_cases/test_cases__utilities.py::test__add_num__001 PASSED
pytest_test/tests/test_cases/test_cases__utilities.py::test__add_num__002 PASSED
pytest_test/tests/test_cases/test_cases__utilities.py::test__add_num__003 FAILED
pytest_test/tests/test_cases/test_cases__utilities.py::test__add_num__004 FAILED
pytest_test/tests/test_cases/test_cases__utilities.py::test__add_num__005 SKIPPED
pytest_test/tests/test_cases/test_cases__utilities.py::test__add_num__006 XFAIL
pytest_test/tests/test_cases/test_cases__utilities.py::test__add_num__007 XPASS

=================================== FAILURES ===================================
______________________________ test__add_num__003 ______________________________
pytest_test/tests/test_cases/test_cases__utilities.py:16: in test__add_num__003
    assert add_num(1, 1) == 1
E   assert 2 == 1
E    +  where 2 = add_num(1, 1)
______________________________ test__add_num__004 ______________________________
pytest_test/tests/test_cases/test_cases__utilities.py:20: in test__add_num__004
    assert add_num(1 + "a") ==1
E   TypeError: unsupported operand type(s) for +: 'int' and 'str'
=========================== short test summary info ============================
SKIPPED [1] pytest_test/tests/test_cases/test_cases__utilities.py:22: スキップ用
XFAIL pytest_test/tests/test_cases/test_cases__utilities.py::test__add_num__006
========= 2 failed, 2 passed, 1 skipped, 1 xfailed, 1 xpassed in 2.44s =========

image.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?