使用 Python 模块 main 函数进行验证测试是一个坏主意吗?
我将很快准确地解释我的意思。
我正在使用 python 开发一个项目,其中有多个模块执行部分工作。举例来说,我有一个名为 Parser.py
的模块,该模块有一个函数 parseFile()
,我的主模块 Main.py
调用该函数为了解析一些文件。
截至目前,我正在 Parser.py 内部使用 main 方法。
if __name__ == "__main__":
line_list = parseFile(sys.argv[1])
out_file = open(sys.argv[2], "w")
for i in range(len(line_list)):
out_file.write(line_list[i].get_string(True))
解析到底做什么并不重要,但重要的是如果您调用它,第一个参数将是解析的输入文件,第二个参数是用于解析的输出文件。
所以,我本质上所做的就是使用批处理文件通过典型的输入、输出、基线系统来验证解析器的结果...
ECHO Set the test, source, input, output and baseline directories
set TESTDIR=%CD%
set SRCDIR=%CD%\..\pypro\src
set INDIR=%CD%\input
set OUTDIR=%CD%\output
set BASEDIR=%CD%\baseline
:: Parser.py main method is base for unit testing on parsing
ECHO Begin Parser testing
cd %INDIR%\Parser
FOR %%G IN (*.psma) DO %SRCDIR%\Parser.py %%G %OUTDIR%\Parser\%%G
ECHO Parser testing complete
cd %TESTDIR%
"C:\Program Files\WinMerge\winmergeU.exe" "%OUTDIR%" "%BASEDIR%"
正如您所看到的,它将结果与基线进行比较,所以如果任何更改程序员都知道它不再有效,或者需求是错误的。
这个方法有什么问题吗?我这样做是因为这很容易。我的计划是继续使用尽可能多的有效且有意义的模块来执行此操作,以及 pydev 内的一套 pyunit 测试...
I'll quickly explain exactly what I mean by this.
I'm working on a project using python, where I have multiple modules doing segments of work. Let's say for example I have a module called Parser.py
and this module has a function parseFile()
which my main module Main.py
calls in order to parse some files.
As of right now, I'm using a main method inside of the Parser.py
if __name__ == "__main__":
line_list = parseFile(sys.argv[1])
out_file = open(sys.argv[2], "w")
for i in range(len(line_list)):
out_file.write(line_list[i].get_string(True))
It's not important what exactly the parsing does, but the important part is if you call it, the first argument will be the input file for the parsing, the second argument is the output file for parsing.
So, what I'm doing essentially, is I'm using a batch file to validate the results of my parser by a typical input, output, baseline system...
ECHO Set the test, source, input, output and baseline directories
set TESTDIR=%CD%
set SRCDIR=%CD%\..\pypro\src
set INDIR=%CD%\input
set OUTDIR=%CD%\output
set BASEDIR=%CD%\baseline
:: Parser.py main method is base for unit testing on parsing
ECHO Begin Parser testing
cd %INDIR%\Parser
FOR %%G IN (*.psma) DO %SRCDIR%\Parser.py %%G %OUTDIR%\Parser\%%G
ECHO Parser testing complete
cd %TESTDIR%
"C:\Program Files\WinMerge\winmergeU.exe" "%OUTDIR%" "%BASEDIR%"
As you can see it diffs the results against the baseline, so if anything is changed the programmer knows it is no longer valid, or the requirements are wrong.
Is there anything wrong with this method? I did it because it would be easy. My plan is to continue doing this with as many modules that I can which are valid and make sense to do this way, as well as a suite of pyunit tests inside pydev...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这是一个好主意,而且它似乎确实是
if __name__ == '__main__'
构造的常见用例。虽然这是一个更常见的结构:这为您提供了在交互式解释器中使用
main
的额外灵活性。 Guido 和其他人提供了一些更好的示例 此处。I think it's a good idea, and it does seem to be a common use case for
if __name__ == '__main__'
construct. Though this is a more usual structure:This gives you the additional flexibility to use your
main
from within the interactive interpreter. There are a few more nice examples from Guido and others here.就我个人而言,我在这些情况下所做的是创建测试用例(尽管这些可能更多地作为集成测试用例而不仅仅是单元测试用例)。
因此,通常(在我的工作流程中),这些将是常规测试用例(它将实际输出与预期输出进行比较)。尽管可能位于单独的源文件夹中,但其运行频率不如单元测试用例。
将其作为
__main__
的不好之处在于,您必须记住将其作为入口点运行,并且随着项目的发展,您可能会忘记执行此操作,并且您有很多这些文件的数量——或者至少有一个调用 main() 的测试用例:)Personally, what I do in these situations is creating test cases (although these would could more as integration test cases and not only unit test cases).
So, usually (in my workflow), those would be regular test cases (which diff the actual output with the expected output). Although probably in a separate source folder which is not run as often as the unit-test cases.
The bad part of having it as the
__main__
is that you'll have to remember to run it as the entry point and you'll probably forget to do it later on as the project grows and you have many of those files -- or at least have a test case that calls that main() :)