Studio
2023.10
バナーの背景画像
Studio ガイド
最終更新日 2024年4月26日

Before および After コンテキスト

テスト ケース内では、テスト ケースの実行前 (Before) と実行後 (After) に特定のアクションを実行できます。これらのコンテキストは、通常、リソースの設定と破棄、ログ記録の実行、およびテスト環境の管理に使用されます。
  • BeforeRunContext (設定): Before の実行は、テスト ケースのメインの実行前にアクションを実行するために使用されます。多くの場合、テスト環境の初期状態の設定やリソースの準備に使用されます。Before コンテキストの一般的なユース ケースには、以下のようなものがあります。
    • テストに必要な変数またはデータを初期化する。
    • 外部システムまたはデータベースへの接続を設定する。
    • テスト実行の開始をログに記録する。
    • テストで使用するアプリケーションまたは Web ページを開く。
    • テスト ケースの実行前に固有の例外を設定する。
  • AfterRunContext (ティアダウン): After (実行後) は、テスト ケースのメイン実行の完了後にアクションを実行するために使用されます。通常、リソースのクリーンアップ、操作の完了、およびテスト結果の記録に使用されます。After コンテキストの一般的なユース ケースは以下のとおりです。
    • テスト中に使用されたアプリケーションまたは Web ページを閉じる。
    • データベース接続やネットワーク リソースなどのリソースを解放する。
    • Before コンテキストとメイン実行で記録された例外をキャッチして出力する。
コード化されたオートメーションでは、実行の前後に実行されるアクションのカスタム インターフェイスを作成することができます。そのためには、メイン クラスに隣接する 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 の実行中に例外がスローされると、実行は終了し、対応する例外がスローされます。
Before (実行前) と After (実行後) の両方で例外が発生した場合、例外は AggregateException にバンドルされます。

実行前後の実装

BeforeAfter を使用すると、コード化されたテスト ケースの前後にアクションを定義して、オートメーションを強化できます。このカスタム実装は、コード化されたテスト ケース内で作成することも、別の部分クラスとして作成することもできます。このチュートリアルでは、設定、破棄、およびログをコード化されたテスト ケースに統合する方法について説明します。

コード化されたテスト ケース内での実装

Before (実行前) および After (実行後) インターフェイスは、コード化されたテスト ケース内に直接実装できます。

  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");
            }

カスタム部分クラスを使用した実装

Before および After 実行のインターフェイスは、カスタム部分クラスを使用してプロジェクト内の特定の .cs ファイルにのみ実装できます。
  1. コード ソース ファイルを作成します。ここに Before および After 実行のインターフェイスを実装します。この例では、ファイルに TestCaseBase という名前を付けます。
    1. public class の横に 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 ファイルでのみ、同じ Before および After 実行のインターフェイスを使用できるようになります。
  2. コード化されたオートメーションを作成します。この例では、TestCase という名前のコード化されたテスト ケースを作成します。カスタム部分クラスの Before および After 実行のインターフェイスを使用するには、コード化されたテスト ケースがこの部分クラスから継承されるようにします。
    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 の部分クラスを使用して実装する

Before および After コンテキスト インターフェイスは、プロジェクト内の他のコード化されたテスト ケースまたはコード化されたワークフローが継承できる CodedWorkflow 部分クラスを使用して実装できます。この実装は、プロジェクト内のすべてのコード化されたテスト ケースとコード化されたワークフローに適用されます。
  1. コード ソース ファイルを作成し、CodedWorkflow.cs とは異なる名前を付けます。そうしないと、部分クラスが既存の読み取り専用クラス CodedWorkflow と競合します。
  2. Before および After コンテキストのカスタム インターフェイスを実装する部分クラスを構築します。
  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 上でホバーして [考えられる修正内容を表示] を選択するか、IBeforeAfterRun を選択してから Ctrl + . キーを押します。
  8. [Implement interface] を選択します。
    これにより、以下のような既定の実装が作成されます。
    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");
            }

Was this page helpful?

サポートを受ける
RPA について学ぶ - オートメーション コース
UiPath コミュニティ フォーラム
UiPath ロゴ (白)
信頼とセキュリティ
© 2005-2024 UiPath. All rights reserved.