【发布时间】:2019-09-04 08:53:10
【问题描述】:
我正在用 Python 和 Selenium 构建一个 QA 系统。我实现了一个很酷的记录器模块,我真的很喜欢它。我的系统运行良好并记录了所有内容(但前提是断言正确)。例如,下面这个测试:
def test_mode_check(self):
""" Tests if the website is in TEST_MODE """
login_page = LoginPage(self.driver)
if login_page.is_test_mode() is True:
assert True
logger.success(TEST_MODE_SUCCESSFUL)
else:
logger.warning(TEST_MODE_FAILURE)
print("i reach this")
assert False
稍微解释一下。 TEST_MODE是我为网站设置的环境变量,应该在测试前设置。因此,它检查它是否打开。 TEST_SUCCESSFUL 和 TEST_FAILURE 只是全局变量,它们包含带有适当消息的字符串。我正在使用的记录器称为“loguru”。此外,login_page.is_test_mode() 只是一个特定于页面的方法,它在页面上查找 TEST_MODE 视觉指示,如果找到则返回 TRUE。这是参考的方法:
def is_test_mode(self):
"""
This function checks if the website is in test mode. The tests should not proceed if it is not in TEST_MODE.
:return: True or False depending on the condition
"""
if self.find_element(*self.locator.IS_TEST_MODE).text == \
"The website is running in test mode. ":
return True
else:
return False
因此,如果设置了 env 变量,则输出如下:
04/09/19 at 11:41:24 AM | SUCCESS | TEST_MODE is set ✓
test_mode_check (tests.individual_tests.tests_preliminary.PreliminaryTests) ... OK (5.645s)
一切都很棒。但是,如果我故意删除 ENV 变量以测试错误日志记录,那么这是唯一发生的事情:
test_mode_check (tests.individual_tests.tests_preliminary.PreliminaryTests) ... ERROR (7.585422)
s
======================================================================
ERROR [7.585422s]: tests.individual_tests.tests_preliminary.PreliminaryTests.test_mode_check
... Traceback ...
# here goes the exception ... which basically says that the element is
# absent from the webpage
根本不显示我的警告消息,它提供更多信息。我还在 else 块 print('i reach this') 中尝试了打印功能,但没有显示任何内容。谁能给我任何提示?如果需要,我可以提供更多信息。
编辑:我正在使用unittest。
【问题讨论】:
-
您是否使用像 pytest 这样的框架来运行您的单元测试?
-
哦,我忘了说。我正在使用默认的 Python 库 -
unittest。
标签: python selenium unit-testing testing qa