Studio
2023.10
False
横幅背景图像
Studio 用户指南
上次更新日期 2024年4月26日

“前”和“后”上下文

在测试用例中,“之前执行”和“之后执行”允许您在运行测试用例之前和之后执行某些操作。这些上下文通常用于设置和关闭资源、执行日志记录以及管理测试环境。
  • BeforeRunContext(设置):“前”执行用于在主要执行测试用例之前执行操作。它通常用于设置测试环境的初始状态和准备资源。“前”上下文的一些常见用例包括:
    • 正在初始化测试所需的变量或数据。
    • 设置与外部系统或数据库的连接。
    • 记录测试执行的开始。
    • 打开与测试交互的应用程序或网页。
    • 在运行测试用例之前设置特定的异常。
  • AfterRunContext(拆卸):“后”执行用于在测试用例的主要执行完成后执行操作。它通常用于清理资源、完成操作以及记录测试结果。“后”上下文的常见用例包括:
    • 关闭测试期间使用的应用程序或网页。
    • 释放资源,例如数据库连接或网络资源。
    • 捕获并打印在“前”上下文和主执行中记录的异常。
在编码自动化中,您可以选择为要在运行之前和之后执行的操作创建自定义界面。这可以通过在主类旁边声明 IBeforeAfterRun 接口,然后为这些操作实现您自己的自定义逻辑来实现。

BeforeRunContext

探索 BeforeRunContext 的定义时, 您可以找到以下 代码片段:
// Context of the run before the execution.
public class BeforeRunContext
{
    // The relative path of the workflow.
    public string RelativeFilePath { get; set; }
}// Context of the run before the execution.
public class BeforeRunContext
{
    // The relative path of the workflow.
    public string RelativeFilePath { get; set; }
}

此上下文表示执行自动化之前的状态。它包含有关正在执行的工作流的相对路径的信息。您可以根据用例在此处设置异常。

AfterRunContext

探索 AfterRunContext 的定义时, 您可以找到以下 代码片段:
// Context of the run after the execution.
public class AfterRunContext
{
    // The relative path of the workflow.
    public string RelativeFilePath { get; set; }

    // The exception caught in execution if any.
    public Exception Exception { get; set; }
}// Context of the run after the execution.
public class AfterRunContext
{
    // The relative path of the workflow.
    public string RelativeFilePath { get; set; }

    // The exception caught in execution if any.
    public Exception Exception { get; set; }
}

此上下文表示执行自动化之后的状态。其中包括有关已执行工作流的相对路径以及执行之前和执行期间捕获的任何异常的详细信息。

异常处理

Before 和主执行并发运行,以捕获执行期间出现的异常。在这些阶段引发的任何异常都将捕获并存储在 AfterRunContext 中,如果没有发生异常,则将其保留为 null。
Before 和主执行运行后,将执行 After 方法。如果在 After 运行期间引发异常,则执行将结束,并引发相应的异常。
如果 BeforeAfter 运行中都发生异常,则会将它们捆绑到 AggregateException 中。

在执行之前和之后实现

“前”和“后”执行可用于定义编码测试用例之前和之后的操作,从而增强自动化。您可以在编码测试用例中创建此自定义实现,也可以将其创建为单独的部分类。在本教程中,了解如何在编码测试用例中集成设置、拆卸和日志记录。

在编码测试用例中实现

您可以直接在编码测试用例中实现“前”执行和“后”执行接口。

  1. 在编码测试用例 所在的公共类中,添加 IBeforeAfterRun 接口。此问题将以红色突出显示,表示可能需要 修复。
    public class MyTest : CodedWorkflow, IBeforeAfterRunpublic class MyTest : CodedWorkflow, IBeforeAfterRun
  2. 选择“显示潜在的修复程序”或按 Ctrl + . ,然后选择“实现接口”。
    这将生成一个默认界面,如下 所示:
    public void After(AfterRunContext context)
            {
                throw new NotImplementedException();
            }
    
            public void Before(BeforeRunContext context)
            {
                throw new NotImplementedException();
            }public void After(AfterRunContext context)
            {
                throw new NotImplementedException();
            }
    
            public void Before(BeforeRunContext context)
            {
                throw new NotImplementedException();
            }
  3. 根据需要修改实现。
    在提供的代码示例中,上下文在执行之前 记录一条消息,并在执行之后检查是否存在 异常。
    public void After(AfterRunContext context)
            {
                if (context.Exception != null)
    			{
    				Log(context.Exception.Message);
    			}
            }
    
            public void Before(BeforeRunContext context)
            {
                Log("this is before");
            }public void After(AfterRunContext context)
            {
                if (context.Exception != null)
    			{
    				Log(context.Exception.Message);
    			}
            }
    
            public void Before(BeforeRunContext context)
            {
                Log("this is before");
            }

使用自定义部分类实现

您可以使用自定义部分类,仅为项目中的某些 .cs 文件实现“前”和“后”执行接口。
  1. 创建代码源文件。您可在此处实现“前”和“后”执行接口。在此示例中,将文件命名为 TestCaseBase
    1. 在公共类旁边添加 IBeforeAfterRun 接口。此问题将以红色突出显示,表示可能需要修复。
      注意:自定义分部类(在此示例中为 TestCaseBase)必须继承 CodedWorkflow 类。这可使继承自定义部分类的其他 cs 文件按预期运行。
      public class TestCaseBase : CodedWorkflow, IBeforeAfterRunpublic class TestCaseBase : CodedWorkflow, IBeforeAfterRun
    2. 选择“显示潜在的修复程序”或按 Ctrl + . ,然后选择“实现接口”。
      这将生成一个默认界面,如下 所示:
      public void After(AfterRunContext context)
              {
                  throw new NotImplementedException();
              }
      
              public void Before(BeforeRunContext context)
              {
                  throw new NotImplementedException();
              }public void After(AfterRunContext context)
              {
                  throw new NotImplementedException();
              }
      
              public void Before(BeforeRunContext context)
              {
                  throw new NotImplementedException();
              }
    3. 根据需要修改实现。

      在提供的示例中,上下文在执行之前记录一条消息,并在执行之后检查是否存在异常。

      public void After(AfterRunContext context)
              {
                  if (context.Exception != null)
                  {
                      throw context.Exception;
                  }
                  else
                  {
                      Log("Test " + context.RelativeFilePath + " finished with no exception.");
                  }
              }
      
              public void Before(BeforeRunContext context)
              {
                  Log("Execution started for " + context.RelativeFilePath);
              }public void After(AfterRunContext context)
              {
                  if (context.Exception != null)
                  {
                      throw context.Exception;
                  }
                  else
                  {
                      Log("Test " + context.RelativeFilePath + " finished with no exception.");
                  }
              }
      
              public void Before(BeforeRunContext context)
              {
                  Log("Execution started for " + context.RelativeFilePath);
              }
    现在,您可以使用此自定义部分类,仅对继承该类的 .cs 文件使用相同的“前”和“后”执行接口。
  2. 创建编码自动化。对于此示例,创建一个名为 TestCase 的编码测试用例。要在自定义部分类中使用相同的“前”和“后”执行接口,请确保编码测试用例继承自此部分类。
    public class TestCase : TestCaseBase
        {
            [TestCase]
            public void Execute()
            {
                Log("Executing the test...");
            }public class TestCase : TestCaseBase
        {
            [TestCase]
            public void Execute()
            {
                Log("Executing the test...");
            }
要演示此特定实现的工作方式,请查看下方的输出日志:
Execution started for file: TestCase.cs
 [Info] IBeforeAfterRunExamples execution started
 [Info] Execution started for TestCase.cs
 [Info] Executing the test...
 [Info] Test TestCase.cs finished with no exception.
 [Info] IBeforeAfterRunExamples execution ended in: 00:00:00 Execution started for file: TestCase.cs
 [Info] IBeforeAfterRunExamples execution started
 [Info] Execution started for TestCase.cs
 [Info] Executing the test...
 [Info] Test TestCase.cs finished with no exception.
 [Info] IBeforeAfterRunExamples execution ended in: 00:00:00

使用 CodedWorkflow 部分类实现

您可以使用项目中任何其他编码测试用例或编码工作流可继承的 CodedWorkflow 部分类来实现“前”和“后”上下文接口。此实现适用于项目中的所有编码测试用例和编码工作流。
  1. 创建一个代码源文件,并将其命名为不同于 CodedWorkflow.cs 的名称,否则部分类将与现有的 CodedWorkflow 只读类冲突。
  2. 构造一个为“前”和“后”上下文实现自定义接口的部分类。
  3. 将类重命名为 CodedWorkflow。否则,您将无法使用可用服务。
  4. 确保已编码源文件中的类继承 CodedWorkflowBase 类。
  5. 在代码源文件的开头添加 UiPath.CodedWorkflows 命名空间 。
    using System;
    using System.Collections.Generic;
    using UiPath.CodedWorkflows;using System;
    using System.Collections.Generic;
    using UiPath.CodedWorkflows;
  6. 在代码源文件的类中 ,包含 IBeforeAfterRun
    namespace TestAutomationProject1
    {
        public partial class CodedWorkflow : CodedWorkflowBase, IBeforeAfterRunnamespace TestAutomationProject1
    {
        public partial class CodedWorkflow : CodedWorkflowBase, IBeforeAfterRun
  7. 将鼠标悬停在 IBeforeAfterRun 上,然后选择“显示可能的修复”,或选择它并按 Ctrl + .
  8. 选择“实现接口”。
    这将在下面创建默认 实现:
    public void After(AfterRunContext context)
            {
                throw new NotImplementedException();
            }
    
            public void Before(BeforeRunContext context)
            {
                throw new NotImplementedException();
            }public void After(AfterRunContext context)
            {
                throw new NotImplementedException();
            }
    
            public void Before(BeforeRunContext context)
            {
                throw new NotImplementedException();
            }
  9. 或者,您可以根据用例进一步自定义界面。在下面的代码示例中,上下文在执行之前打印出一条消息,在执行之后,它会检查是否引发了任何异常并再次打印输出。
    public void After(AfterRunContext context)
            {
                if (context.Exception != null)
    			{
    				Log(context.Exception.Message);
    			}
            }
    
            public void Before(BeforeRunContext context)
            {
                Log("this is before");
            }public void After(AfterRunContext context)
            {
                if (context.Exception != null)
    			{
    				Log(context.Exception.Message);
    			}
            }
    
            public void Before(BeforeRunContext context)
            {
                Log("this is before");
            }

此页面是否有帮助?

获取您需要的帮助
了解 RPA - 自动化课程
UiPath Community 论坛
Uipath 白色徽标
信任与安全
© 2005-2024 UiPath. All rights reserved.