【问题标题】:Coded UI Continue on failure Assertions编码的 UI 继续失败断言
【发布时间】:2015-01-19 17:23:24
【问题描述】:

您好,我知道我要问的问题已经被其他人问过了,但是我仍然不清楚那些已经在网上发布的内容,因此我发布这个问题是为了澄清我的疑问。希望大家能帮帮我。

目前我正在使用 Microsoft Visual Studio 2013 Premium。我正在使用记录和播放功能。我记录了一些动作和一些验证点。现在,当验证点失败时,脚本将立即停止。但是,即使某些点失败,我也希望脚本继续运行。我在网上阅读了一些选项,但是我不知道应该将它们放在我的脚本中的哪个位置。 我看到了这篇文章Coded UI - "Continue on failure" for Assertions 但是我没有使用 SpecFlow 是否仍然适用于我?我应该把这些代码放在哪个部分?在我的方法里面?创建一个新方法?还是?

bool thisTestFailed = false;
if ( ... the first assertion ... ) { thisTestFailed = true; }
if ( ... another assertion ... ) { thisTestFailed = true; }
if ( ... and another assertion ... ) { thisTestFailed = true; }
if ( thisTestFailed ) {
Assert.Fail("A suitable test failed message");
}"

【问题讨论】:

  • 一个断言,并不意味着使用 Assert.SomeCondition()。只需使用简单的条件来设置 thisTestFailed。

标签: c# coded-ui-tests assertions


【解决方案1】:

一个断言,并不意味着使用Assert.SomeCondition()。只需使用简单的条件来设置thisTestFailed。 Assert 类抛出一个AssertFailedException 来表示失败。不应捕获此异常。此异常由单元测试引擎处理以指示断言失败。

bool thisTestFailed = false;
if ( {someCondition} ) { thisTestFailed = true; }
if ( !{someOtherConditionWhichShouldBeFalse} ) { thisTestFailed = true; }
if ( {yetAnotherCondition} ) { thisTestFailed = true; }
if ( thisTestFailed ) {
    Assert.Fail("A suitable test failed message");
}"

甚至更简单:

bool thisTestFailed = {someCondition} ||
 !{someOtherConditionWhichShouldBeFalse} ||
 {yetAnotherCondition}
Assert.IsFalse(thisTestFailed, "A suitable test failed message");

这样你就得到了所有的“测试”,但只有一个Assert.Fail

【讨论】:

  • 嗯.. 那么当断言失败时如何防止我的脚本停止呢?
  • 所以我应该使用这个而不是上面的方法? if ( thisTestFailed ) { Assert.Fail("A suitable test failed message"); }
  • 如果需要,否。最初,如果所有测试都“表现”,则 testFailed 的值将保持为假。所以你的 Assert 会认为它是假的,如果不是,它将显示作为 Assert.IsFalse() 的第二个参数给出的消息。
【解决方案2】:

链接的问题确实提到了 Specflow,但问题的详细信息和接受的答案不依赖于 Specflow。

通常由 Coded UI record 和 generate 创建的断言方法有点像:

void AssertionMethod1()
{
    ... get the values of fieldOne, fieldTwo, etc
    Assert.Equals(fieldOne, fieldOneExpectedValue);
    Assert.Equals(fieldTwo, fieldTwoExpectedValue);
    Assert.Equals(fieldThree, fieldThreeExpectedValue);
    ... more calls of assert methods.
}

使用的断言方法和访问字段值的机制可能会更复杂。

每个Assert.Equals(...) 都使用类似于以下目的的代码实现:

void Assert.Equals(string actual, string expected)
{
    if ( actual == expected )
    {
        // Pass. Nothing more to do.
    }
    else
    {
        // Fail the test now.
        throw AnAssertionFailureException(...);
    }
}

因此,链接的问题建议将对记录的断言方法的调用(即从CodedUItestMethod1 调用AssertionMethod1)更改为:

... get the values of fieldOne, fieldTwo, etc
if(fieldOne != fieldOneExpectedValue) { thisTestFailed = true; }
if(fieldTwo != fieldTwoExpectedValue) { thisTestFailed = true; }
if(fieldThree != fieldThreeExpectedValue) { thisTestFailed = true; }
... etc.

虽然更简单的方法可能是将AssertionMethod1 复制到另一个文件中,然后将其修改为与上述类似。 Coded UI 中的 UIMap 编辑器有一个“将代码移动到 UIMap.cs”命令(通过上下文菜单或命令图标访问),它将方法移动到 uimap.cs 文件中,从而允许您根据需要对其进行编辑。自动生成的代码进入uimap.designer.cs 文件,不应编辑此文件。它和uimap.cs 文件的顶部附近都有partial class UIMap,因此它们的组合内容构成了该类。 uimap.cs 文件用于添加到 UI 地图。

【讨论】:

    猜你喜欢
    • 2013-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    • 2010-12-15
    • 2011-10-13
    • 2011-06-11
    相关资源
    最近更新 更多