相关疑难解决方法(0)

Python单元测试:测试失败时自动运行调试器

有没有办法在单元测试失败时自动启动调试器?

现在我只是手动使用pdb.set_trace(),但这非常繁琐,因为我需要每次添加它并在结束时将其取出.

例如:

import unittest

class tests(unittest.TestCase):

    def setUp(self):
        pass

    def test_trigger_pdb(self):
        #this is the way I do it now
        try:
            assert 1==0
        except AssertionError:
            import pdb
            pdb.set_trace()

    def test_no_trigger(self):
        #this is the way I would like to do it:
        a=1
        b=2
        assert a==b
        #magically, pdb would start here
        #so that I could inspect the values of a and b

if __name__=='__main__':
    #In the documentation the unittest.TestCase has a debug() method
    #but I don't understand how to use it
    #A=tests() …
Run Code Online (Sandbox Code Playgroud)

python unit-testing pdb python-unittest

39
推荐指数
2
解决办法
2万
查看次数

使用 testbook 测试 Jupyter notebook 单元时如何修补 input()?

我参与的一个项目使用testbook来测试 Jupyter 笔记本的代码单元。修补工作正常吗?—?除非要测试的代码要求用户输入input(). 我只是不知道如何正确修补它。

使用的版本:Python:3.8.10,测试书:0.4.2

要在 Jupyter 代码单元中测试的代码,标记为name_checking

def fix(text):
    return text.strip().title()

def check(text):
    return len(text) > 1

firstname = input("What's your first name?")
lastname = input("What's your last name?")
fixed_first = fix(firstname)
fixed_last = fix(lastname)
if check(fixed_first) and check(fixed_last):
    print(f"Your name is {fixed_first} {fixed_last}.")
else:
    print("You entered an invalid name.")
Run Code Online (Sandbox Code Playgroud)

尝试 1:测试代码补丁 builtins.input

@testbook(path)
def test_name_checking1(tb): # execute cell tagged "name_checking"
    with tb.patch("builtins.input") as mock_input:
        mock_input.return_value = ["   haRrY   ", " …
Run Code Online (Sandbox Code Playgroud)

python testing monkeypatching jupyter-notebook testbook

3
推荐指数
1
解决办法
29
查看次数