对Jupyter笔记本中的功能进行单元测试? [英] Unit tests for functions in a Jupyter notebook?

查看:27
本文介绍了对Jupyter笔记本中的功能进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Jupyter笔记本,我计划重复运行它。它里面有函数,代码结构是这样的:

def construct_url(data):
    ...
    return url

def scrape_url(url):
    ... # fetch url, extract data
    return parsed_data

for i in mylist: 
    url = construct_url(i)
    data = scrape_url(url)
    ... # use the data to do analysis

我想为construct_urlscrape_url编写测试。执行此操作最明智的方法是什么?

我考虑过的一些方法:

  • 将函数移到实用程序文件中,并在某个标准Python测试库中为该实用程序文件编写测试。可能是最佳选择,但这意味着并非所有代码在笔记本中都可见。
  • 使用测试数据在笔记本本身内写入断言(向笔记本添加噪音)。
  • 使用专门的Jupyter测试来测试单元格的内容(不要认为这是可行的,因为单元格的内容将会更改)。

推荐答案

Python标准测试工具,如doctestunittest可以直接在笔记本中使用。

Doctest

文档字符串中包含函数和测试用例的笔记本单元格:

def add(a, b):
    '''
    This is a test:
    >>> add(2, 2)
    5
    '''
    return a + b

运行文档字符串中所有测试用例的笔记本单元格(笔记本中的最后一个):

import doctest
doctest.testmod(verbose=True)

输出:

Trying:
    add(2, 2)
Expecting:
    5
**********************************************************************
File "__main__", line 4, in __main__.add
Failed example:
    add(2, 2)
Expected:
    5
Got:
    4
1 items had no tests:
    __main__
**********************************************************************
1 items had failures:
   1 of   1 in __main__.add
1 tests in 2 items.
0 passed and 1 failed.
***Test Failed*** 1 failures.

单元测试

带功能的笔记本单元格:

def add(a, b):
    return a + b
包含测试用例的笔记本单元格(笔记本中的最后一个)。单元格中的最后一行在执行单元格时运行测试用例:

import unittest

class TestNotebook(unittest.TestCase):
    
    def test_add(self):
        self.assertEqual(add(2, 2), 5)
        

unittest.main(argv=[''], verbosity=2, exit=False)

输出:

test_add (__main__.TestNotebook) ... FAIL

======================================================================
FAIL: test_add (__main__.TestNotebook)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython-input-15-4409ad9ffaea>", line 6, in test_add
    self.assertEqual(add(2, 2), 5)
AssertionError: 4 != 5

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (failures=1)

调试失败的测试

调试失败的测试时,在某个时刻停止测试用例执行并运行调试器通常很有用。为此,请在希望停止执行的行前插入以下代码:

import pdb; pdb.set_trace()

例如:

def add(a, b):
    '''
    This is the test:
    >>> add(2, 2)
    5
    '''
    import pdb; pdb.set_trace()
    return a + b

对于此示例,下次运行doctest时,执行将在RETURN语句之前停止,而Python debugger(Pdb)将启动。您将在笔记本中直接看到PDB提示,允许您检查ab的值、跳过行等。

注意:从Python 3.7开始,可以使用内置的breakpoint()代替import pdb; pdb.set_trace()

我用刚才描述的技术创建了一个Jupyter notebook for experimenting。您可以使用

试用

这篇关于对Jupyter笔记本中的功能进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆