Python mock Patch os.environ 和返回值

时间:2022-10-14
本文介绍了Python mock Patch os.environ 和返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

使用模拟单元测试 conn():

Unit testing conn() using mock:

app.py

import mysql.connector
import os, urlparse


def conn():
    if "DATABASE_URL" in os.environ:
        url = urlparse(os.environ["DATABASE_URL"])
        g.db = mysql.connector.connect(
            user=url.username,
            password=url.password,
            host=url.hostname,
            database=url.path[1:],
        )
    else:
        return "Error"

test.py

def test_conn(self):
    with patch(app.mysql.connector) as mock_mysql:
        with patch(app.os.environ) as mock_environ:
            con()
            mock_mysql.connect.assert_callled_with("credentials")

错误:断言 mock_mysql.connect.assert_called_with 未被调用.

我认为这是因为Database_url"不在我修补的 os.environ 中,并且因为没有对 mysql_mock.connect 进行测试调用.

which i believe it is because 'Database_url' is not in my patched os.environ and because of that test call is not made to mysql_mock.connect.

问题:

1 我需要进行哪些更改才能使此测试代码正常工作?

1 what changes i need to make to make this test code work?

2.我也必须修补'urlparse'吗?

2.Do i also have to patch 'urlparse'?

推荐答案

你可以试试unittest.mock.patch.dict 解决方案.只需使用 dummy 参数调用 conn:

You can try unittest.mock.patch.dict solution. Just call conn with a dummy argument:

import mysql.connector
import os, urlparse


@mock.patch.dict(os.environ, {"DATABASE_URL": "mytemp"}, clear=True)  # why need clear=True explained here https://stackoverflow.com/a/67477901/248616
def conn(mock_A):
    print os.environ["mytemp"]
    if "DATABASE_URL" in os.environ:
        url = urlparse(os.environ["DATABASE_URL"])
        g.db = mysql.connector.connect(
            user=url.username,
            password=url.password,
            host=url.hostname,
            database=url.path[1:],
        )
    else:
        return "Error"

或者,如果您不想修改原始功能,请尝试以下解决方案:

Or if you don't want to modify your original function try this solution:

def func():
    print os.environ["mytemp"]


def test_func():
    k = mock.patch.dict(os.environ, {"mytemp": "mytemp"})
    k.start()
    func()
    k.stop()


test_func()

这篇关于Python mock Patch os.environ 和返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一条:python mock - 在不妨碍实现的情况下修补方法 下一条:使用 mock patch 模拟实例方法

相关文章

最新文章