使用Python进行依赖性测试 [英] Dependency Testing with Python

查看:293
本文介绍了使用Python进行依赖性测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写单元测试来测试两个python包之间是否存在依赖关系。例如:

I would like to write unit tests to test whether a dependency exists between two python packages. E.g.:

a/
    __init__.py
    models.py
    views.py
    ...
b/
    __init__.py
    models.py
    views.py
    ...

单元测试,以检查包 b 中的模块不要从包中的模块导入任何内容 A 。到目前为止,我唯一的解决方案是扫描文件,并检查源代码中是否有from a或import a。还有其他方法吗?其中一个要求是 a / b / 必须在同一目录级别。

a unit test to check that modules in package b don't import anything from modules in package a. The only solution I have so far is to scan the files and check that there isn't a "from a" or "import a" in the source code. Are there other ways of doing this? One of the requirements is that a/ and b/ must be in the same directory level.

我想要进行单元测试,因为我想确保我可以在其他项目中使用包 b c $ c> a ,还没有其他开发人员编写代码,使 b 依赖于 a

I would like to have this unit test because I want to be sure that I can use package b in other projects without package a, and also not have other developers write code that will make b dependent on a.

推荐答案

Python太动态,无法正确执行此操作。考虑您可以通过调用 __ import __ 导入模块,该参数需要一个字符串参数,以便可以在运行时构建要导入的模块的名称。另外, __ import __ 是一个函数,所以它可以绑定到其他名称,所以你甚至不能确定检测所有的情况,当某个正在导入。

Python is too dynamic to do this 100% correctly. Consider that you can import modules by calling __import__, which takes a string argument so the name of the module to import can be constructed at runtime. Also, __import__ is a function, so it could be bound to other names, so you can't even be sure of detecting all the cases when something is being imported.

从技术上讲,模块可以从另一个模块调用函数,该模块导入一个模块并返回。所以你绝对不能通过分析包 b

And it's technically possible for a module to call a function from another module, which imports a module and returns it. So you definitely can't do this by analysing just package b.

那么就有 exec 来执行在运行时构建的任意python代码...

And then there's exec to execute arbitrary python code constructed at runtime...

最接近你可能得到的当 a PYTHONPATH b $ c>,而当 a 不在 PYTHONPATH 时。仍然不是万无一失的,因为这仅仅告诉你, b 完成了所有的测试,没有 a PYTHONPATH ,而不是它不需要任何东西 a 。而如果你真的不幸运,那么在飞行中, b 做了一些真正的愚蠢的事情,并且在$ code> sys.path 导入 a 无论如何,

The closest you could get is probably to try and make your unit test exercise b when a is on the PYTHONPATH, and also when a isn't on the PYTHONPATH. Still not foolproof, as that only tells you that b completed all the tests without a on the PYTHONPATH, not that it doesn't ever need a for anything. And if you're really unlucky, b does something really stupid and fiddles with sys.path in flight and manages to import a anyway somehow.

然而,如果这是你自己的代码,你知道你不会做这样一个古怪的垃圾,然后一个简单的脚本扫描文件 import 语句可能是你最好的选择。它可能会非常频繁地在随机的其他人的代码。完全没有办法完全完成这项工作。

If, however, this is all your your own code and you know you don't do this kind of wacky crap, then a simple script that scans the files for import statements is probably your best bet. It would probably work very very often on random other people's code too. It's just not possible to do the job perfectly with full generality.

这篇关于使用Python进行依赖性测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆