Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.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测试模块执行_Python_Mocking_Pytest_Python Unittest - Fatal编程技术网

Python 用pytest测试模块执行

Python 用pytest测试模块执行,python,mocking,pytest,python-unittest,Python,Mocking,Pytest,Python Unittest,我有一个python模块,它作为python-myu包执行。myu模块--arg1a1--arg2a2。它在本地磁盘上创建一个工件,并将该工件上传到云存储桶 我的模块基本上是这样的 import argparse def func(): # parse args # create artifact # upload to bucket func() #test_module.py from unittest import mock def test_my_modu

我有一个python模块,它作为
python-myu包执行。myu模块--arg1a1--arg2a2
。它在本地磁盘上创建一个工件,并将该工件上传到云存储桶

我的模块基本上是这样的

import argparse

def func():
    # parse args
    # create artifact
    # upload to bucket

func()
#test_module.py
from unittest import mock

def test_my_module():
    test_args = # [  test args ]
    with mock.patch.object(sys, 'argv', test_args):
        import my_package.my_module
        # test file is written to local dir
#test_module.py
from unittest import mock
import my_package.my_module

@mock.patch('my_package.my_module.cloud_client')
def test_my_module():
    # run test
我想为这个模块编写一个测试,我可以从不同的脚本运行,比如说
test\u module.py
。我想模拟云api,这样就不会上传任何内容,但也会向模块提供一组测试命令行参数

到目前为止,我可以做这样的事情

import argparse

def func():
    # parse args
    # create artifact
    # upload to bucket

func()
#test_module.py
from unittest import mock

def test_my_module():
    test_args = # [  test args ]
    with mock.patch.object(sys, 'argv', test_args):
        import my_package.my_module
        # test file is written to local dir
#test_module.py
from unittest import mock
import my_package.my_module

@mock.patch('my_package.my_module.cloud_client')
def test_my_module():
    # run test
这里的问题是,如果不先导入
myu包,我就无法模拟云api。myu模块运行模块中的代码并上载工件。如果我这样做了
像这样的

import argparse

def func():
    # parse args
    # create artifact
    # upload to bucket

func()
#test_module.py
from unittest import mock

def test_my_module():
    test_args = # [  test args ]
    with mock.patch.object(sys, 'argv', test_args):
        import my_package.my_module
        # test file is written to local dir
#test_module.py
from unittest import mock
import my_package.my_module

@mock.patch('my_package.my_module.cloud_client')
def test_my_module():
    # run test

然后,代码将在第一条导入语句中执行
import my\u package.my\u module
。是否有一种解决方案,可以在导入对象所在的模块之前模拟该对象?

使用惯用的解决方案解决此问题:

import argparse

def func():
    # parse args
    # create artifact
    # upload to bucket

# Ensure this function only runs when this module is being executed as main, not when imported.
if __name__ == '__main__':
    func()
您可以在此处找到更多详细信息:


在测试中导入(但不运行)模块后,您可以设置模拟并执行测试。

Ah当然可以。非常感谢。我确信运行带有-m标志的模块会改变
\uuuu name\uuuu
变量(我一定是在很久以前错误地想到了这一点),所以我甚至没有费心测试它。谢谢