1

c# を使用して、実行時にテストケースのテスト カテゴリを見つけようとしています。MSTEST を使用していますが、TestContext には TestCategory に関連する情報がありません。TestCategory 情報をキャプチャ/ログに記録したいと考えています。私の場合、テストケースに複数の TestCATEGORIES が割り当てられています。例

BaseTest には Initialization メソッドと CleanUp メソッドがあります。

[TestClass]
    public class CustomerTest : BaseTest
    {
        [TestMethod]
        [TestCategory("Smoke")]
        [TestCategory("regression")]
        public void Login()
4

1 に答える 1

2

次のように、リフレクションを使用してテスト メソッド内の属性を取得できます。

[TestMethod]
[TestCategory("Smoke")]
[TestCategory("regression")]
public void Login()
{
    var method = MethodBase.GetCurrentMethod();
    foreach(var attribute in (IEnumerable<TestCategoryAttribute>)method
        .GetCustomAttributes(typeof(TestCategoryAttribute), true))
    {
        foreach(var category in attribute.TestCategories)
        {
            Console.WriteLine(category);
        }
    }
    var categories = attribute.TestCategories;  
}

テストメソッド内以外の場所でカテゴリを取得したい場合は、使用できます

var method = typeof(TheTestClass).GetMethod("Login");

メソッドベースを取得し、上記の属性を取得します。

ソース:メソッドの属性の値を読み取る

于 2015-10-09T19:07:30.530 に答える