【发布时间】:2019-11-19 16:20:06
【问题描述】:
目录结构:
app\features\login.feature
app\features\__init__.py
app\features\steps\__init__.py
app\features\steps\login.py
app\fixtures\__init__.py
app\fixtures\fixtures.py
app\models\__init__.py
app\models\login.py
这是我文件中的内容。当我在 from ...models.login import Login 上运行行为 app\features\steps\login.py 时,以下文件出现错误
from behave import *
from ...models.login import Login
from ...models.footer import Footer
@given("unauthenticated user loads the home page")
def step_impl(context):
assert Login.is_sign_in_submit_displayed(context, Login.signinbutton)
@step("sign-in button is clicked")
def step_impl(context, signinbutton):
Login.tap_sign_in(context, Login.signinbutton)
@when("login form is populated with valid credentials")
def step_impl(context):
Login.enter_email(context, Login.email, DEFAULT_EMAIL)
Login.enter_password(context, Login.password, DEFAULT_PASSWORD)
@then("login is successful")
def step_impl(context):
assert Login.home_page.is_rewards_displayed(context, Footer.menubutton)
assert Login.home_page.is_account_displayed(context, Footer.accountbutton
这是在 app\models\login.py 中找不到的文件:
class Login():
"""Login screen object definitions"""
signinbutton = ".//*[@text='SIGN IN']"
email = ".//*[contains(@class, 'EditText')]"
password = "(.//*[@class='android.widget.EditText'])[2]"
def is_sign_in_submit_displayed(self, signinbutton):
"""Is the 'SIGN IN' button in the 'Login Screen' displayed?"""
self.browser.implicitly_wait(30)
return self.browser.find_element_by_xpath(signinbutton)
def enter_email(self, emailelement, email):
"""Enter text into 'Email' Field in the 'Login Screen'"""
self.browser.find_element_by_xpath(emailelement).send_keys(email)
def enter_password(self, passelement, password):
"""Enter text into 'Password' Field in the 'Login Screen"""
self.browser.find_element_by_xpath(passelement).send_keys(password)
def click_sign_in_submit(self, signinbutton, menubutton, accountButton):
"""Click Sign in button and await Home Screen Nav Footer showing 'ACCOUNT' Button appearing"""
self.browser.tap(signinbutton)
self.browser.find_element_by_xpath(menubutton)
self.browser.find_element_by_xpath(accountButton)
我正在关注此站点上的文档 - https://docs.python.org/2.5/whatsnew/pep-328.html
但是当我运行这个应用程序时,它返回错误"KeyError: "'__name__' not in globals"
我搜索了 SO 线程,但似乎都没有解决这个问题。我的 IDE Pycharm 似乎认为导入没问题,如果我稍微更改它,它会将其突出显示为错误。
完整的堆栈跟踪:
MacBook-Pro:app jasonlegako$ behave
Exception KeyError: "'__name__' not in globals"
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/bin/behave", line 10, in <module>
sys.exit(main())
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/behave/__main__.py", line 183, in main
return run_behave(config)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/behave/__main__.py", line 127, in run_behave
failed = runner.run()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/behave/runner.py", line 804, in run
return self.run_with_paths()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/behave/runner.py", line 809, in run_with_paths
self.load_step_definitions()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/behave/runner.py", line 796, in load_step_definitions
load_step_modules(step_paths)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/behave/runner_util.py", line 412, in load_step_modules
exec_file(os.path.join(path, name), step_module_globals)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/behave/runner_util.py", line 386, in exec_file
exec(code, globals_, locals_)
File "features/steps/login.py", line 2, in <module>
from ...app.models.login import Login
KeyError: "'__name__' not in globals"
【问题讨论】:
-
我试过了:
from ...app.models.login import Login和from ..models.login import Login两者都返回相同的错误。 -
您可以编辑您的问题以包含 完整 堆栈跟踪吗?
-
更新了查找
-
我认为这是因为您试图通过
behave进行相对导入,这似乎无法处理它们。我猜它什么时候运行exec_file,没有生成__name__,所以相对导入失败。 -
嗨,你解决过这个问题吗?我有一个非常相似的问题
标签: python python-behave