使用/运行 python 2to3 作为或类似单元测试
我使用 2to3 实用程序从命令行转换代码。我想做的就是将其基本上作为单元测试运行。即使它测试的是文件而不是部件(函数、方法...),这对于单元测试来说是正常的。
它不需要是一个单元测试,我也不知道如何自动转换文件,我只是想在像庄园这样的单元测试中监视文件的 py3 合规性。我似乎找不到这方面的任何文档或示例。
一个例子和/或文档会很棒。
I have used the 2to3 utility to convert code from the command line. What I would like to do is run it basically as a unittest. Even if it tests the file rather than parts(functions, methods...) as would be normal for a unittest.
It does not need to be a unittest and I don't what to automatically convert the files I just want to monitor the py3 compliance of files in a unittest like manor. I can't seem to find any documentation or examples for this.
An example and/or documentation would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需使用 python2.6+ 的
-3
选项即可获知 Python3 合规性。Simply use the
-3
option with python2.6+ to be informed of Python3 compliance.如果您尝试验证代码是否可以在 Python 3.x 中工作,我建议使用一个脚本,将源文件复制到新目录,对其运行
2to3
,然后将单元测试复制到目录并运行它们。这可能看起来有点不优雅,但却符合单元测试的精神。您正在做出一系列您认为关于代码的外部行为应该正确的断言,无论实现如何。如果转换后的代码通过了您的单元测试,您可以考虑您的代码支持 Python 3。
If you are trying to verify the code will work in Python 3.x, I would suggest a script that copies the source files to a new directory, runs
2to3
on them, then copies the unit tests to the directory and runs them.This may seem slightly inelegant, but is consistent with the spirit of unit testing. You are making a series of assertions that you believe ought to be true about the external behavior of the code, regardless of implementation. If the converted code passes your unit tests, you can consider your code to support Python 3.