3
2

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 3 years have passed since last update.

DatabricksのNotebookの単体テストを行う

Posted at

はじめに

以下のNotebookの単体テストを行います

MyNotebook
def hoge(i):
  return 'hoge'*i

def fuga(i):
  return 'fuga'*i

#テスト用Notebookの作成
NotebookMyNotebookと同じフォルダに、テスト用NotebookMyNotebookTestを作成します

MyNotebookTest
# Cmd1 
%run "./MyNotebook"

# Cmd2
import unittest

class MyNotebookTests(unittest.TestCase):
  
  def test_hoge(self):
    self.assertEqual(hoge(3), 'hogehogehoge')
    self.assertNotEqual(hoge(2), 'hoge')

  def test_fuga(self):
    self.assertEqual(fuga(3), 'fugafugafuga')
    self.assertNotEqual(fuga(2), 'fuga')

suite = unittest.TestLoader().loadTestsFromTestCase(MyNotebookTests)
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)

作成するポイントとして、

  • Cmd 1で先に%runコマンドを実行してテスト対象となるNotebookを実行します
%run "./MyUnittest"
  • Cmd 2以降でpythonテストフレームワークであるunittestを用いてテストケースを書きます

実行結果

以下のようにテストOKとなることを確認します

test_fuga (__main__.MyNotebookTests) ... ok
test_hoge (__main__.MyNotebookTests) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK
Out[50]: <unittest.runner.TextTestResult run=2 errors=0 failures=0>
3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?