近日,使用Pytest+Appium 实现APP端UI自动化,遇到Pytest收集用例失败的情况。
报错信息如下:
test_room.py:None (test_room.py)
ImportError while importing test module '/Users/XXXX/case/test_room.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
test_room.py:5: in <module>
from Page.init_page import InitPage
../Page/init_page.py:8: in <module>
from Page.home_page import HomePage
../Page/home_page.py:4: in <module>
from Page.room_page import RoomPage
../Page/room_page.py:5: in <module>
from Page.home_page import HomePage
E ImportError: cannot import name 'HomePage' from 'Page.home_page' (/Users/XXXX/Page/home_page.py)
目录结构如下:
报错原因分析:
主要是因为home_page.py和room_page.py这两个文件内的类之间有相互调用引起的
- home_page.py涉及代码:
from Page.room_page import RoomPage
class HomePage(BasePage):
def goto_room(self):
self.find_element_until_visibility(self._room).click()
return RoomPage(self.driver)
- room_page.py涉及代码:
from Page.home_page import HomePage
class RoomPage(BasePage):
def back_home_page(self):
self.find_element_until_visibility(self._back_btn).click()
return HomePage(self.driver)
解决办法
直接将room_page.py涉及到的HomePage的引用直接放到特定的函数中引用,而不是整个文件的引用
- 解决后room_page.py涉及代码:
class RoomPage(BasePage):
def back_home_page(self):
self.find_element_until_visibility(self._back_btn).click()
from Page.home_page import HomePage
return HomePage(self.driver)