以下代码无法运行,但是如果我删除“datafile = sys.argv [1]”这一行就可以了。我将脚本调用为:
generateTests.py完整路径到文件
错误消息位于代码示例下方。有人可以解释这样做有什么问题吗?
#!/usr/bin/python
import sys
import unittest
import xmlrunner
mydata = [["VectorAdd", "56"],["MatrixMul", "101"]]
class TestWithDataFiles(unittest.TestCase):
def setUp(self):
print("Initializing...")
@classmethod
def setUpClass(cls):
# Now call unittest version
super(TestWithDataFiles, cls).setUpClass()
print("Class setup...")
datafile = sys.argv[1]
def myClass(TestWithDataFiles):
@classmethod
def setUpClass(cls):
# Call TestWithDataFiles class setup first
super(myClass, cls).setUpClass()
def test_genHSAILRunToBreakpoint(testData):
def test_HSAILRunToBreakpoint(self):
print("%s"%testData[0])
print(self)
return test_HSAILRunToBreakpoint
def test_genHSAILRunToTwoBreakpoints(testData):
def test_HSAILRunToTwoBreakpoints(self):
print("%s"%testData[1])
return test_HSAILRunToTwoBreakpoints
if __name__ == '__main__':
for testData in mydata:
test_name = "test_HSAILRunToBreakpoint_%s"%testData[0]
test = test_genHSAILRunToBreakpoint(testData)
setattr(TestWithDataFiles, test_name, test)
test_name = "test_HSAILRunToTwoBreakpoints_%s"%testData[0]
test = test_genHSAILRunToTwoBreakpoints(testData)
setattr(TestWithDataFiles, test_name, test)
unittest.main(testRunner=xmlrunner.XMLTestRunner(output="testlog"))
python ~/bin/generateTests.py ~/.profile
Traceback (most recent call last):
File "/home/bgriffin/bin/generateTests.py", line 47, in <module>
unittest.main(testRunner=xmlrunner.XMLTestRunner(output="testlog"))
File "/usr/lib/python2.7/unittest/main.py", line 94, in __init__
self.parseArgs(argv)
File "/usr/lib/python2.7/unittest/main.py", line 149, in parseArgs
self.createTests()
File "/usr/lib/python2.7/unittest/main.py", line 158, in createTests
self.module)
File "/usr/lib/python2.7/unittest/loader.py", line 130, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "/usr/lib/python2.7/unittest/loader.py", line 100, in loadTestsFromName
parent, obj = obj, getattr(obj, part)
AttributeError: 'module' object has no attribute '/home/bgriffin/'
以下代码修改仍然失败。请注意,我尝试在suite.addTests调用中同时使用test_name和test,两者都具有相同的行为。
#!/usr/bin/python
import argparse
import sys
import unittest
import xmlrunner
mydata = [["VectorAdd", "56"],["MatrixMul", "101"]]
class TestWithDataFiles(unittedst.TestCase):
def setUp(self):
print("Initializing...")
@classmethod
def setUpClass(cls):
# Now call unittest version
super(TestWithDataFiles, cls).setUpClass()
print("Class setup...")
dataFile = "teswt"
arg1 = sys.argv[1]
print(arg1)
def myClass(TestWithDataFiles):
@classmethod
def setUpClass(cls):
# Call TestWithDataFiles class setup first
super(myClass, cls).setUpClass()
def test_genHSAILRunToBreakpoint(testData):
def test_HSAILRunToBreakpoint(self):
print("%s"%testData[0])
print(self)
return test_HSAILRunToBreakpoint
def test_genHSAILRunToTwoBreakpoints(testData):
def test_HSAILRunToTwoBreakpoints(self):
print("%s"%testData[1])
return test_HSAILRunToTwoBreakpoints
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--useValgrind', help="run hsail-gdb through valgrind")
parser.add_argument('--branch', action='store', type=str, default="main", help="perforce branch to run tests from")
parser.add_argument('--datafiles', action='store', type=str, help="Comma separated list of test data files")
args = parser.parse_args()
print(args.datafiles)
suite = unittest.TestSuite()
for testData in mydata:
test_name = "test_HSAILRunToBreakpoint_%s"%testData[0]
test = test_genHSAILRunToBreakpoint(testData)
setattr(TestWithDataFiles, test_name, test)
suite.addTest(myClass(test))
test_name = "test_HSAILRunToTwoBreakpoints_%s"%testData[0]
test = test_genHSAILRunToTwoBreakpoints(testData)
setattr(TestWithDataFiles, test_name, test)
suite.addTest(myClass(test))
results = xmlrunner.XMLTestRunner(output="testlog").run(suite)
python ~/bin/generateTests.py --datafiles ~/.profile/home/bgriffin/.profile
Traceback (most recent call last):
File "/home/bgriffin/bin/generateTests.py", line 54, in <module>
suite.addTest(myClass(test))
File "/usr/lib/python2.7/unittest/suite.py", line 49, in addTest
raise TypeError("{} is not callable".format(repr(test)))
TypeError: None is not callable
答案 0 :(得分:0)
以下是我的最终解决方案。我使用argparse来定义和解析命令行选项,然后在调用unittest.main()
之前删除除sys.argv [0]之外的所有sys.argv值。#!/usr/bin/python
import argparse
import sys
import unittest
import xmlrunner
mydata = [["VectorAdd", "56"],["MatrixMul", "101"]]
class TestWithDataFiles(unittest.TestCase):
def setUp(self):
print("Initializing...")
@classmethod
def setUpClass(cls):
# Now call unittest version
super(TestWithDataFiles, cls).setUpClass()
print("Class setup...")
def myClass(TestWithDataFiles):
@classmethod
def setUpClass(cls):
# Call TestWithDataFiles class setup first
super(myClass, cls).setUpClass()
def test_genHSAILRunToBreakpoint(testData):
def test_HSAILRunToBreakpoint(self):
print("%s"%testData[0])
print(self)
return test_HSAILRunToBreakpoint
def test_genHSAILRunToTwoBreakpoints(testData):
def test_HSAILRunToTwoBreakpoints(self):
print("%s"%testData[1])
return test_HSAILRunToTwoBreakpoints
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--useValgrind', help="run hsail-gdb through valgrind")
parser.add_argument('--branch', action='store', type=str, default="main", help="perforce branch to run tests from")
parser.add_argument('--datafiles', action='store', type=str, help="Comma separated list of test data files")
args = parser.parse_args()
print(args.datafiles)
del sys.argv[1:]
for testData in mydata:
test_name = "test_HSAILRunToBreakpoint_%s"%testData[0]
test = test_genHSAILRunToBreakpoint(testData)
setattr(TestWithDataFiles, test_name, test)
test_name = "test_HSAILRunToTwoBreakpoints_%s"%testData[0]
test = test_genHSAILRunToTwoBreakpoints(testData)
setattr(TestWithDataFiles, test_name, test)
unittest.main(testRunner=xmlrunner.XMLTestRunner(output="testlog"))