Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在我的代码上编写pytest或unittest?_Python_Unit Testing_Pytest_Python Unittest_Python Mock - Fatal编程技术网

Python 如何在我的代码上编写pytest或unittest?

Python 如何在我的代码上编写pytest或unittest?,python,unit-testing,pytest,python-unittest,python-mock,Python,Unit Testing,Pytest,Python Unittest,Python Mock,我不熟悉单元测试。我试图测试函数是否被调用,以及下面的计划时间是否正确 python文件名为#schedule.py 试试这个 def test_screenshot(mocker): url = 'url' path = /your/path/ mocker.patch('screenshot.env', return_value = (url, path)) mocker.patch('screenshot.email_it', return_va

我不熟悉单元测试。我试图测试函数是否被调用,以及下面的计划时间是否正确

python文件名为#schedule.py

试试这个

def test_screenshot(mocker):
    url = 'url'
    path = /your/path/

    mocker.patch('screenshot.env', return_value = (url, path))
    
    mocker.patch('screenshot.email_it', return_value = (path))

    screenshot()

将文件名更改为test_schedule.py,pytest将使用test_*扫描文件,您需要添加断言语句,以将测试用例标记为通过或失败我在这里看到的几个问题。我建议你读这本书。最初的指南应该给你一个很好的介绍。指出代码中的几个问题以提供帮助:在
测试捕获
中,您试图调用
断言(assert)调用(called)一次
。但是,您永远不会“模仿”schedule.function1.capture中的任何内容。因此,尝试从实际代码中访问
mock
方法
assert\u called\u once
将不起作用。另外,
assert\u called\u once
是一种方法,因此当您使事情正常运行时,请确保调用它:
assert\u called\u once()
。第二个测试文件
test_schedule2.py
中要指出的另一项。您的
断言
未正确使用等式。您想使用
=
而不是
=
。单
=
表示赋值,双
=
表示相等。@idjaw你能帮我举个例子,说明如何在代码中使用mock吗?
def test_screenshot(mocker):
    url = 'url'
    path = /your/path/

    mocker.patch('screenshot.env', return_value = (url, path))
    
    mocker.patch('screenshot.email_it', return_value = (path))

    screenshot()