背景: 我用 Python 编写 Squish GUI 测试。我尝试尽可能使测试代码成为 Pythonic 和 DRY,因此我将所有重复代码移动到单独的类/模块中。
问题定义: test.verify 或 assert 语句告诉调试器在语句所在的行停止,在大多数情况下,这是包含单个测试步骤详细信息的模块。该行在eclipse中手动运行时显示,在Jenkins中自动测试输出。
要实际查看测试失败的原因,最好在带有断言的过程调用点停止调试器。然后测试人员/GUI 开发人员可以发现 GUI 上的哪些操作导致了问题以及检查了什么。
示例:
测试抽象.py
class App():
def open_file(self, filename):
pass # example
def test_file_content(content):
# squish magic to get file content from textbox etc.
# ...
test.verify(content in textBoxText)
test_file_opening.py
def main():
app = App()
app.open_file('filename.txt')
app.test_file_content('lorem')
由于测试在 test.verify() 调用上失败,调试器停止并定向到 test_abstract.py 文件。它实际上没有说明导致此测试失败的测试步骤。
有没有办法告诉调试器忽略测试失败的直接位置,并让它显示调用测试过程的位置。我正在寻找不需要在通用测试文件本身中使用太多代码的优雅方式。
不是理想的有效解决方案: 现在我没有在抽象模块内部使用 test.verify 并在特定的测试用例代码中调用它。广义测试函数返回一个元组 (test_result, test_descriptive_message_with error) 用 * 解压:
def test_file_content(content):
# test code
return (result, 'Test failed because...')
测试用例代码包含:
test.verify(*test_file_content('lorem'))
效果很好,但是每个测试用例代码都必须包含很多 test.verify(*... 并且测试开发人员必须记住它。更不用说它看起来很湿...(不是干)。
请您参考如下方法:
是的!如果您可以访问 Squish 6,则有一些新功能可以做到这一点。 fixateResultContext() 函数将导致所有结果被重写,使它们看起来像是起源于祖先框架。 See Documentation .
如果您使用的是 python,则可以将其包装到 handy context manager 中
def resultsReportedAtCallsite(ancestorLevel = 1):
class Ctx:
def __enter__(self):
test.fixateResultContext(ancestorLevel + 1)
def __exit__(self, exc_type, exc_value, traceback):
test.restoreResultContext()
return Ctx()
def libraryFunction():
with resultsReportedAtCallsite():
test.compare("Apples", "Oranges")
以后对 libraryFunction() 的任何失败调用都将指向包含 libraryFunction() 的代码行,而不是 test.compare() 内。
