python 动态获取调用函数的函数名
上一篇 /
下一篇 2017-01-09 15:53:42
/ 个人分类:python
背景:
在断言的模块中,出错后,截图,截图名想知道是哪个用例产生的,所以需要
动态获取
经过查询有个inspcet,能够动态获取动态模块名
printinspect.stack()
得到列表:
[(<frame. object at 0x7f83b973ac50>, '/Users/shylock/demo/testing/kodak_appium/test/test.py', 38, 'function_one', [' print inspect.stack()\n'], 0), (<frame. object at 0x7f83b9522a90>, '/Users/shylock/demo/testing/kodak_appium/test/test.py', 41, '<module>', [' myclass.function_one()\n'], 0)]
经过分析得:
inspect.stack()[0][3]得到的是固定的模块名
inspect.stack()[1][3]得到的是动态的模块名
但是在断言部分直接用,获取的是,断言模块名,所以只能最后在原函数中,传参数给断言。
成果:
AssertAct.py
defAssertAct(self, first, second, msg=None, model=None):
try:
assert(first == second)
print'yes'
except:
now = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))
day = time.strftime('%Y-%m-%d', time.localtime(time.time()))
screenpath ='../ScreenShot/'+ day
screenshot =screenpath +'/'+ model + now +'.png'
ifos.path.exists(screenpath):
self.driver.get_screenshot_as_file(screenshot)
raiseIOError, msg
else:
os.mkdir(screenpath)
self.driver.get_screenshot_as_file(screenshot)
raiseIOError, msg
Calc.py
from tool import get_mod
deftest_calc(self):
self.driver.start_activity('com.google.android.calculator','com.android.calculator2.Calculator')
self.driver.find_element_by_id("com.google.android.calculator:id/digit_4").click()
res =self.driver.find_element_by_id("com.google.android.calculator:id/formula").text
AssertAct(self, res,'5','the result of calc is not correct',get_mod())
tool.py
defget_mod():
returninspect.stack()[1][3]
最终获取文件名为:test_calc2017-01-09-15-47-50.png
相关阅读:
- webdriver python log 操作 (ice1016, 2016-12-20)
- webdriver python log 操作 (ice1016, 2016-12-20)
- webdriver python css 操作 (ice1016, 2016-12-22)
- webdriver python 学习总结 (ice1016, 2016-12-22)
- python+pycharm环境安装与配置(一) (xianrenqiu, 2016-12-22)
- selenium+python1--环境搭建 (jxf-ekz, 2016-12-23)
- webdriver python 异常处理 操作 (ice1016, 2016-12-26)
- JMeter非GUI方式运行时动态设置线程组及传参 (simmy.wu, 2016-12-27)
- python声明了 coding = utf-8不生效 (123_456, 2017-1-05)
- Python自动化学习思路 (博为峰网校, 2017-1-06)
收藏
举报
TAG:
Python
python
动态
动态函数名