我有Python中的测试文件wirtten:test1.py test2.py。。。 在执行它们之前,我需要用一个名为初始化.py这需要争论。你知道吗
测试文件必须尽可能地轻和易于编写。你知道吗
我想创建一个脚本:
我想了几个办法:
总而言之:这就好像我有100个文件以相同的25行开头,我想把这25行放在一个文件中,每次导入它们。你知道吗
另一种方式是查看3个文件:
#File1.py
var1 = sys.argv(1)
#File2.py
var2 = var1+"second"
#File3.py
var3 = var1+var2+"third"
print var3
我想先开始./File1.py 得到“第一秒第三”
我成功了
#! /usr/bin/python
import sys
import subprocess
source_content = "#! /usr/bin/python\n"+"import sys\n"
sourcefile = "file2.py"
txt_file = open(sourcefile)
source_content += txt_file.read()
sourcefile = "file3.py"
txt_file = open(sourcefile)
source_content += txt_file.read()
destinationfile = "Copyfile2.py"
target = open (destinationfile, 'w')
target.write(source_content)
target.close()
chmodFile = "chmod 777 "+destinationfile
chmod = subprocess.Popen(chmodFile, shell=True)
chmod.wait()
arguments = str("./"+destinationfile+" ")
arguments += " ".join(map(str,sys.argv[1:len(sys.argv)]))
startTest = subprocess.Popen(arguments, shell=True)
startTest.wait()
但我不得不删除“#”!/usr/bin/python”,并将var1测试并重命名为系统参数[1] 在这些文件上。 我觉得这不是个好办法。。。你知道吗
你用unittest模块怎么样?你知道吗
现在您可以在每个测试文件中继承BaseTest。你知道吗
或者,如果你想远离标准的单元测试方法。。。你知道吗
假设目录结构:
一些命名约定:
run
的函数来运行测试生成一个名为run的脚本_测试.py(或者类似的…)
现在您的测试文件中完全不需要锅炉板代码。只要你遵循这个公式
相关问题 更多 >
编程相关推荐