环境: Opensuse Linux 42.3,python 3.4.6,在eclipse下运行
对于自动测试系统,我从测试用例中生成python脚本。 执行脚本会遇到错误
NameError: name 'setVariable' is not defined
生成的脚本导入名为testfunc的模块,该模块定义了函数:
testfunc.py:
import sys
print("defining 'saveDeviceStatus'")
def saveDeviceStatus(devicePath,fq_fileName):
print("ssaveDeviceStatus:")
print("device '%s'" % devicePath)
print("filePath '%s'" % fq_fileName)
print("defining 'setVariable'")
def setVariable(path,value):
print("setVariable:")
print("path '%s'" % path)
print("value '%s'" % value)
print("defining 'compareDeviceStatus'")
def compareDeviceStatus(device,filename,options):
print("compareDeviceStatus:")
print("device '%s'" % device)
print("filename '%s'" % filename)
print(options)
print("defining 'checkResults'")
def checkResults(checkList):
print("checkResults:")
print(checkList)
生成的脚本如下所示:
t_0030_1206_001.py:
#!/usr/bin/python3
#
import time
import os,sys
import importlib.util
spec = importlib.util.spec_from_file_location("testFunctions", "../")
sys.path.append("/home/heinrich/git/swisbox/swibTestPy/pyLib/")
import testfunc
os.chdir('/home/heinrich/git/swisbox/swib61850/test/tryAutomation/nanopi/pilot_dal-static-lib/180525T093506/')
sys.path.append('/home/heinrich/git/swisbox/swibTestPy/src/testlinkFiles/')
# draw actual status
saveDeviceStatus("SWIBCLS1","beforeSwitch_to_1")
checkResults([
compareDeviceStatus("SWIBCLS1","beforeSwitch_to_1",["nodate"])
])
执行脚本输出
$ ./t_0030_1206_001.py
defining 'saveDeviceStatus'
defining 'setVariable'
defining 'compareDeviceStatus'
defining 'checkResults'
Traceback (most recent call last):
File "./t_0030_1206_001.py", line 17, in <module>
saveDeviceStatus("SWIBCLS1","beforeSwitch_to_1")
NameError: name 'saveDeviceStatus' is not defined
$
虽然执行了打印语句,(对我来说)表明函数已定义,但调用它们会导致错误。我在这里错过了什么?我可以运行哪些额外的诊断程序?
答案 0 :(得分:0)
这可能有所帮助:
mod.py:
def my_fun():
print ("Hello, world")
main.py:
import mod
my_func()
执行
python3 main.py
Traceback (most recent call last):
File "main.py", line 3, in <module>
my_func()
NameError: name 'my_func' is not defined
您可以通过导入功能
来修复from mod import my_fun
或使用:
mod.my_fun()
因此,在您的情况下,请使用:testfunc.saveDeviceStatus(..)
或使用from testfunc import saveDeviceStatus
答案 1 :(得分:0)
首先,该错误正在抱怨saveDeviceStatus
,而不是setVariable
。
saveDeviceStatus
。你做导入testfunc
;所以你应该通过这个名称来引用这些功能。
testfunc.saveDeviceStatus("SWIBCLS1","beforeSwitch_to_1")
testfunc.checkResults([
testfunc.compareDeviceStatus("SWIBCLS1","beforeSwitch_to_1",["nodate"])
])
另请注意,这一切都非常不顺便;您应该使用lower_case_with_underscores
作为函数名称,而不是camelCase
。