Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.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

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代码如何判断它是否';它在测试中运行_Python_Unit Testing - Fatal编程技术网

一段python代码如何判断它是否';它在测试中运行

一段python代码如何判断它是否';它在测试中运行,python,unit-testing,Python,Unit Testing,我有一个使用Python unittest模块进行单元测试的大型项目 我有一个控制系统行为的大方面的小方法。我需要这个方法在UTs下运行时返回一个固定的结果,以提供一致的测试运行,但是对于我来说,为每个UT模拟这个结果是昂贵的 有没有一种方法可以让这个单一的方法,unittest感知,这样它就可以在unittest下运行时修改它的行为?我确信还有其他更好的方法,但是您可以始终从main设置一个全局标志,而不是在unittest下,然后在方法中访问它 当然,另一种方法是作为单元测试设置的一部分覆盖

我有一个使用Python unittest模块进行单元测试的大型项目

我有一个控制系统行为的大方面的小方法。我需要这个方法在UTs下运行时返回一个固定的结果,以提供一致的测试运行,但是对于我来说,为每个UT模拟这个结果是昂贵的


有没有一种方法可以让这个单一的方法,unittest感知,这样它就可以在unittest下运行时修改它的行为?

我确信还有其他更好的方法,但是您可以始终从main设置一个全局标志,而不是在unittest下,然后在方法中访问它


当然,另一种方法是作为单元测试设置的一部分覆盖该方法-如果您的方法被称为
brian
,并且您有一个
test\u brian
,那么只需在您的预测试设置期间
brian=test\u brian
即可,您可能需要在前面的语句中输入模块名称。

您可以在运行时仅为测试修改函数。例如:

TEST_FLAG=true python -m unittest discover -s tests -b
MONGODB_URI =
    os.environ.get('MONGODB_URI') if not os.environ.get('TEST_FLAG') 
        else os.environ.get('MONGODB_TEST_URI')
模块.py test.py
现在,无论何时
模块中的代码调用
func()
,它实际上都会调用您的
替换函数

我对
unittest
模块知之甚少,但是如果您直接为单元测试运行文件,则可以在测试代码中包含以下内容:

if __name__ == "__main__":
if语句中的任何代码只有在直接调用特定模块时才会执行,而不会导入到其他模块中。根据文档,这就是您首先应该调用的
unittest.main()

这假定您不是从命令行运行

编辑:您可以查看函数堆栈,尝试找到
unittest.main()
函数

import inspect

def in_unit_test():
  current_stack = inspect.stack()
  for stack_frame in current_stack:
    for program_line in stack_frame[4]:    # This element of the stack frame contains 
      if "unittest" in program_line:       # some contextual program lines
        return True
  return False


这是一个有点粗糙的解决方案,但是
inspect
模块有很多有用的内省功能。

我的解决方案是在运行
unittest
之前设置一个
TEST\u FLAG=true
环境变量。例如:

TEST_FLAG=true python -m unittest discover -s tests -b
MONGODB_URI =
    os.environ.get('MONGODB_URI') if not os.environ.get('TEST_FLAG') 
        else os.environ.get('MONGODB_TEST_URI')
然后,只需检查变量是否已设置。例如:

TEST_FLAG=true python -m unittest discover -s tests -b
MONGODB_URI =
    os.environ.get('MONGODB_URI') if not os.environ.get('TEST_FLAG') 
        else os.environ.get('MONGODB_TEST_URI')

使用
tox
我设置了如下环境变量:

[testenv]
setenv = TOX_TESTENV = true
然后在代码中检查变量是否已设置:

导入操作系统
如果os.env.get('TOX_TESTENV'):
#代码正在测试中运行。
#例如,这对于配置日志级别非常有用。

您可以检查
unittest
模块是否已加载。仅当测试运行时才应加载它

>>> 'unittest' in sys.modules.keys()
False
>>> from unittest import TestCase
>>> 'unittest' in sys.modules.keys()
True

我的解决方案是在上面的“if name…”行之后创建一个标志“sys.unittesting=None”。sys被导入到许多文件中。。。当然,任何通常导入的模块都可以。我发现您有时确实需要确定您是否正在进行单元测试,例如,抑制等待用户输入的内容。警告:此函数将始终返回
True
,因为函数名在单元测试中为
,如果程序行中的“unittest”为
,则将由
断言:
哇,已修复。谢谢。如果您跳过
.keys()
,只在sys.modules
中使用
'unittest',它应该会更有效。