刚才当我的程序跑 Pytest 的时候,提示:
Hint: make sure your test modules/packages have valid Python names.
ImportError: cannot import name 'maintabpage.py'
排查了好一会儿还是没找到问题所在,类与类之间的调用逻辑是对的,代码也没写错。
后来百度搜索了一下,发现有人在网上也提出了同样的问题,并提供了最终的解决方案。先赶紧做个笔记,以便后期自己查阅。
对方的问题及方案如下:
Pytest 执行用例报 Hint: make sure your test modules/packages have valid Python names.
近日,使用 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)
根据对方提供的解决方案,我把所有需要调用的类,直接写在特定的函数体中引用:
class signpage(mainpage):
# 点击登录按钮
def click_signin(self):
self.find_ID(self.v_signIn).click()
from YUELI.Page.maintab_page import maintabpage
return maintabpage(self.driver)
class realinfopage(mainpage):
# 返回上一级
def click_return_back(self):
self.find_ID(self.return_back).click()
from YUELI.Page.maintab_page import maintabpage
return maintabpage(self.driver)
class maintabpage(mainpage):
# 点击个人头像
def click_avatar(self):
self.find_ID(self.avatar).click()
from YUELI.Page.my_profie_page import myprofilepage
return myprofilepage(self.driver)
# 点击实名按钮
def click_real_name(self):
self.find_ID(self.real_name).click()
from YUELI.Page.real_info_page import realinfopage
return realinfopage(self.driver)
# 点击学籍信息按钮
def click_childroll(self):
self.find_XPATH(self.childRoll).click()
from YUELI.Page.children_list_page import childlistpage
return childlistpage(self.driver)
class studentrollpage(mainpage):
# 返回学籍信息简单页
def click_returnback(self):
self.find_ID(self.return_back).click()
from YUELI.Page.maintab_page import maintabpage
return maintabpage(self.driver)
class childlistpage(mainpage):
# 点击查看学籍按钮
def check_childroll(self):
self.find_ID(self.review_studentnumber).click()
from YUELI.Page.student_roll_page import studentrollpage
return studentrollpage(self.driver)
# 返回我的页面
def click_back_to_me(self):
self.find_ID(self.return_backtome).click()
from YUELI.Page.maintab_page import maintabpage
return maintabpage(self.driver)
修改以后,再重新跑 Pytest,成功运行。