测试函数是否被调用

Test that a function is called

我正在我的应用程序中编写测试,以测试是否调用了某个方法。这是 Python 3.4.3 和 pytest-2.9.2 中的 运行。我是 PyTest 的新手,但对 RSpec 和 Jasmine 非常熟悉。我不确定如何设置测试以便测试 imaplib。IMAP4_SSL 被调用。

我的应用结构:

/myApp
  __init__.py
  /shared
    __init__.py
    email_handler.py
  /tests
    __init__.py
    test_email_handler.py

email_handler.py

import imaplib
def email_conn(host):
    mail = imaplib.IMAP4_SSL(host)  
    return mail;

目前我的测试结果: test_email_handler.py

import sys   
sys.path.append('.')  

from shared import email_handler 

def test_email_handler():   
     email_handler.email_conn.imaplib.IMAP4_SSL.assert_called_once 

这显然失败了。我如何设置此测试以便测试是否调用了 imaplib.IMAP4_SSL?或者是否有更好的方法在我的应用程序中设置测试套件,以便更有效地支持测试?

你能做的是:

email_handler.py

import imaplib

def email_conn(host):
    print("We are in email_conn()")
    mail = imaplib.IMAP4_SSL(host)
    print(mail)
    return mail;

test_email_handler.py

import sys   
sys.path.append('.')  

from shared import email_handler 

def test_email_handler():
    print("We are in test_email_handler()")
    email_handler.email_conn.imaplib.IMAP4_SSL.assert_called_once
    print(email_handler.email_conn.imaplib.IMAP4_SSL.assert_called_once) # this will give you the result of the function (in theory) 

基本上,您所做的就是打印函数 returns。如果没有错误,函数应该已经执行了。

您还可以做的是修改 imaplib 的源代码,以便在您调用的函数中放置打印。

祝你好运!

这听起来像是一个代码覆盖问题:这一行是否被执行了?

对于python覆盖工具是:https://coverage.readthedocs.io

Pytest基于那个工具构建的插件,非常方便: https://pypi.python.org/pypi/pytest-cov

这是一个使用 Python 3.5.2 标准库中的 unittest.mock 的示例:

test_email_handler.py

import sys
from unittest import mock
sys.path.append('.')

from shared import email_handler

@mock.patch.object(email_handler.imaplib, 'IMAP4_SSL')
def test_email_handler(mock_IMAP4_SSL):
    host = 'somefakehost'
    email_handler.email_conn(host)
    mock_IMAP4_SSL.assert_called_once_with(host)

请注意 @mock.patch.object 装饰器将 IMAP4_SSL 替换为模拟对象,该模拟对象作为参数添加。 Mock 是一个强大的测试工具,它可能会让新用户感到困惑。我推荐以下内容以供进一步阅读:

https://www.toptal.com/python/an-introduction-to-mocking-in-python

http://engineroom.trackmaven.com/blog/mocking-mistakes/

http://alexmarandon.com/articles/python_mock_gotchas/