运行 python 虚拟环境中的鼻子测试
Running python nose tests in virtual-env
我有一个脚本 go.py
,它需要 运行 在几个不同的模块上进行单元测试(带鼻子),每个模块都有自己的虚拟环境。
如何在测试前激活每个虚拟环境,并在测试后停用它?
即我想这样做(伪代码):
for fn in functions_to_test:
activate(path_to_env)
run_test(fn)
deactivate()
激活()
在虚拟环境中,有./bin/activate_this.py
这就是我想要的。所以在 go.py
我说
import os
activate_this_file = os.path.join(env_dir,'bin/deactivate_this.py')
execfile(activate_this_file, dict(__file__=activate_this_file))
run_test()
我目前 run_test()
使用
suite_x = TestLoader().loadTestsFromName(test_module + ":" + test_class)
r = run(suite = suite_x, argv = [sys.argv[0], "--verbosity=0", "-s"])
停用
这是我想不通的部分。
env/bin/activate_this.py
的停用等价物是多少?
更广泛的背景
每个模块都将通过 go.py
作为 lambda 函数上传到 AWS。 (其中 'lambda function' 在 AWS 上下文中具有特定含义,与 lambda x:foo(x)
无关)
我希望在各自的虚拟环境中对每个 lambda 函数进行 go.py
到 运行 单元测试(因为一旦部署到 AWS,它们就会在这些虚拟环境中执行)。每个 lambda 函数使用不同的库,因此它们具有不同的虚拟环境。
activate_this.py
脚本不用于在计算过程中切换虚拟环境。它应该在您的流程开始时尽快使用,并且永远不会再被触及。如果您查看脚本的内容,您会发现它没有为将来的停用记录任何内容。一旦 activate_this.py
脚本完成,解释器在脚本开始之前的状态就会丢失。此外,文档还 warns (强调已添加):
Also, this cannot undo the activation of other environments, or modules that have been imported. You shouldn’t try to, for instance, activate an environment before a web request; you should activate one environment as early as possible, and not do it again in that process.
而不是您希望使用的方法,我会让编排器生成特定于需要使用的虚拟环境的 python 解释器(带有 subprocess
),并且向它传递测试 运行ner("nosetests",大概)以及它在该环境中找到 运行 所需的测试所需的参数。
没有一种简单、完整和通用的方法可以做到这一点。原因是 activate_this.py 不仅修改了模块搜索路径,而且它还在 python 过程中 does site configurations with site.addsitedir(), which may execute sitecustomize or usercustomize。相比之下,shell 脚本版本的 activate 只需修改环境变量,并让每个 python 进程自己重新执行站点自定义,因此清理起来要容易得多。
如何解决这个问题?有几种可能:
您可能希望 运行 在 tox 下进行测试。这是我认为最首选的解决方案。
如果你确定你的virtualenv中的none个包有不可逆的sitecustomize/usercustomize,你可以写一个deactivate()来撤销virtualenv对[=36=的修改]、os.environ 和 sys.prefix 或在 activate() 中记住这些值的一个,因此 deactivate() 可以撤消它们。
您可以在 execfile("activate_this.py")
之前的 activate()
中 fork or create a subprocess。要停用虚拟环境,只需 return 到父进程即可。您必须弄清楚子进程如何 return 测试结果,以便 parent/main 进程可以编译最终测试报告。
我有一个脚本 go.py
,它需要 运行 在几个不同的模块上进行单元测试(带鼻子),每个模块都有自己的虚拟环境。
如何在测试前激活每个虚拟环境,并在测试后停用它?
即我想这样做(伪代码):
for fn in functions_to_test:
activate(path_to_env)
run_test(fn)
deactivate()
激活()
在虚拟环境中,有./bin/activate_this.py
这就是我想要的。所以在 go.py
我说
import os
activate_this_file = os.path.join(env_dir,'bin/deactivate_this.py')
execfile(activate_this_file, dict(__file__=activate_this_file))
run_test()
我目前 run_test()
使用
suite_x = TestLoader().loadTestsFromName(test_module + ":" + test_class)
r = run(suite = suite_x, argv = [sys.argv[0], "--verbosity=0", "-s"])
停用
这是我想不通的部分。
env/bin/activate_this.py
的停用等价物是多少?
更广泛的背景
每个模块都将通过 go.py
作为 lambda 函数上传到 AWS。 (其中 'lambda function' 在 AWS 上下文中具有特定含义,与 lambda x:foo(x)
无关)
我希望在各自的虚拟环境中对每个 lambda 函数进行 go.py
到 运行 单元测试(因为一旦部署到 AWS,它们就会在这些虚拟环境中执行)。每个 lambda 函数使用不同的库,因此它们具有不同的虚拟环境。
activate_this.py
脚本不用于在计算过程中切换虚拟环境。它应该在您的流程开始时尽快使用,并且永远不会再被触及。如果您查看脚本的内容,您会发现它没有为将来的停用记录任何内容。一旦 activate_this.py
脚本完成,解释器在脚本开始之前的状态就会丢失。此外,文档还 warns (强调已添加):
Also, this cannot undo the activation of other environments, or modules that have been imported. You shouldn’t try to, for instance, activate an environment before a web request; you should activate one environment as early as possible, and not do it again in that process.
而不是您希望使用的方法,我会让编排器生成特定于需要使用的虚拟环境的 python 解释器(带有 subprocess
),并且向它传递测试 运行ner("nosetests",大概)以及它在该环境中找到 运行 所需的测试所需的参数。
没有一种简单、完整和通用的方法可以做到这一点。原因是 activate_this.py 不仅修改了模块搜索路径,而且它还在 python 过程中 does site configurations with site.addsitedir(), which may execute sitecustomize or usercustomize。相比之下,shell 脚本版本的 activate 只需修改环境变量,并让每个 python 进程自己重新执行站点自定义,因此清理起来要容易得多。
如何解决这个问题?有几种可能:
您可能希望 运行 在 tox 下进行测试。这是我认为最首选的解决方案。
如果你确定你的virtualenv中的none个包有不可逆的sitecustomize/usercustomize,你可以写一个deactivate()来撤销virtualenv对[=36=的修改]、os.environ 和 sys.prefix 或在 activate() 中记住这些值的一个,因此 deactivate() 可以撤消它们。
您可以在
execfile("activate_this.py")
之前的activate()
中 fork or create a subprocess。要停用虚拟环境,只需 return 到父进程即可。您必须弄清楚子进程如何 return 测试结果,以便 parent/main 进程可以编译最终测试报告。