如何在量角器(打字稿)中制作更具描述性的错误日志消息

时间:2019-10-17 13:00:18

标签: typescript testing error-handling protractor

我们正在针对生产网站运行量角器(用打字稿写成)。量角器测试失败时,有人知道如何输出一些日志消息吗?我现在所拥有的基本上只是一个是/否消息,就像这样:

10) Query page accuracy test. Comparison of page data to data extracted from excel. Test begins...
  Message:
    Expected false to be truthy.
  Stack:
    Error: Failed expectation
        at C:\xampp\htdocs\kap\frontend\src\app\qa\qa.js:147:103
        at step (C:\xampp\htdocs\kap\frontend\src\app\qa\qa.js:33:23)
        at Object.next (C:\...

等 这是我们测试的行:

expect(htmlValue == excelValue || (htmlValue === "0" && excelValue == "NaN")).toBeTruthy();

如果测试失败,如何添加更具描述性的消息?谢谢任何人,我在量角器中非常陌生,但是我之前已经做过QA自动化测试。

3 个答案:

答案 0 :(得分:1)

您可以像这样在ToBeTruthy中添加说明:

expect(htmlValue == excelValue || (htmlValue === "0" && excelValue == "NaN")).toBeTruthy("description why I checked this")

如果您愿意,可以显示以下失败的测试值:

expect(
  htmlValue == excelValue || (htmlValue === '0' && excelValue == 'NaN'),
).toBeTruthy(
  `HTML differ form Excel. HTML: ${htmlValue}; Excel: ${excelValue}`,
);

几乎所有的茉莉花Matchers都接受expectationFailOuput参数:

toBe(expected: any, expectationFailOutput?: any): Promise<void>;
toEqual(expected: any, expectationFailOutput?: any): Promise<void>;
toMatch(expected: string | RegExp | Promise<string | RegExp>, expectationFailOutput?: any): Promise<void>;
toBeDefined(expectationFailOutput?: any): Promise<void>;
toBeUndefined(expectationFailOutput?: any): Promise<void>;
toBeNull(expectationFailOutput?: any): Promise<void>;
toBeTruthy(expectationFailOutput?: any): Promise<void>;
toBeFalsy(expectationFailOutput?: any): Promise<void>;
toContain(expected: any, expectationFailOutput?: any): Promise<void>;
toBeLessThan(expected: number | Promise<number>, expectationFailOutput?: any): Promise<void>;
toBeLessThanOrEqual(expected: number | Promise<number>, expectationFailOutput?: any): Promise<void>;
toBeGreaterThan(expected: number | Promise<number>, expectationFailOutput?: any): Promise<void>;
toBeGreaterThanOrEqual(expected: number | Promise<number>, expectationFailOutput?: any): Promise<void>;
toBeCloseTo(expected: number | Promise<number>, precision?: any, expectationFailOutput?: any): Promise<void>;

您将在jasminewd2 depot

中找到Matchers定义。

答案 1 :(得分:0)

您几乎可以在所有茉莉花匹配器中添加自定义消息,例如

expect(true).toBe(true, "This will be shown if the expectation will fail")

expect(htmlValue == excelValue || (htmlValue === "0" && excelValue == "NaN")).toBeTruthy(`Expected ${excelValue}` but got ${htmlValue });

答案 2 :(得分:0)

虽然提出的解决方案可能有效,但使用它们并不是一个好习惯,仅仅是因为它需要更多的键入内容和不可读的代码

默认框架正式支持的两个选项是Jasmine:

  1. 对于Protractor 6+,您可以使用内置方法.withContext() 不幸的是,Jasmine团队没有提供太多文档。但是一般语法是expect(true).withContext("your description").toBe(false);,并且可以用于任何Jasmine断言,而不仅是.toBeTruthy()

  2. 对于版本号<6的量角器,当每个断言带有两个参数时-一个未记录的功能-第一个是期望值,第二个-失败消息。例如

expect("first value").toBe("second value", "verification of some values")

失败并显示以下消息

- Expected 'first value' to be 'second value', 'verification of some values'.