如何在运行时获取TestPropertyAttribute的值 [英] How to get the value of TestPropertyAttribute in runtime

查看:196
本文介绍了如何在运行时获取TestPropertyAttribute的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有MS测试的SpecFlow来自动化BDD测试用例。当我们使用Scenaio-Outline和示例时,MS测试使用variant inbuild构建场景名称。我想在运行时调用该变体名称以进行报告。有人可以在这里帮助

I am using SpecFlow with MS test for automating BDD test cases. When we are using Scenaio-Outline with examples, the MS test consturcting the scenario name with variant inbuild. I want to call that variant name during runtime for reporting purpose. Can someone please help here.

我想读取"VariantName"的值。在运行时。

I want to read the value of "VariantName" at run time.

[Microsoft.VisualStudio.TestTools.UnitTesting.TestPropertyAttribute(" VariantName"," 1")]

详细信息:


我有一个Scenario大纲像下面

I have a Scenario outline like below

Feature: Test Reporting
    Scenario Outline:  Verify pushing data to Event hub using API concept
       Given User Insert Geo Infomation to LoadCollection '<ScenarioId>'

Examples:
| ScenarioId | 
| Scenario_00 | 
| Scenario_01 |
| Scenario_02 | 



在testExplorer中(我正在使用MSTest)我看到如下情况

In testExplorer(I am using MSTest) i am seeing the scenarios like below

TestReportingFeature
-->VerifyPushingDataToEventHubUsingAPIConcept_Scenario_00
-->VerifyPushingDataToEventHubUsingAPIConcept_Scenario_01
-->VerifyPushingDataToEventHubUsingAPIConcept_Scenario_02



我编码如下获取测试名称

I Coded like below to get the test name

var title = ScenarioContext.Current.ScenarioInfo.Title;



以上代码返回类似  VerifyPushingDataToEventHubUsingAPIConcept    Scenario_00  是
没有附加。

above code returning like VerifyPushingDataToEventHubUsingAPIConcept the Scenario_00 are not getting appended.


我期待  VerifyPushingDataToEventHubUsingAPIConcept_Scenario_00  要退回,
请帮我解决问题。

I am expecting VerifyPushingDataToEventHubUsingAPIConcept_Scenario_00 to be returned, Please help me how to get it.

推荐答案

嗨朋友,

欢迎来到MSDN论坛。

Welcome to MSDN forum.

>>我想在运行时读取"VariantName"的值。[Microsoft.VisualStudio.TestTools.UnitTesting.TestPropertyAttribute(" VariantName"," 1")]

>> I want to read the value of "VariantName" at run time. [Microsoft.VisualStudio.TestTools.UnitTesting.TestPropertyAttribute("VariantName", "1")]

为此,您可以使用GetCustomAttributes方法获取值。示例代码如下:

For this, you can use GetCustomAttributes method to get the value. Sample code are as below:

[TestClass]
    public class UnitTest1
    {
        [TestProperty("VariantName", "1")]
        [TestMethod]
        public void MyTestMethod()
        {
            var myAttribute = GetType().GetMethod("MyTestMethod").GetCustomAttributes(true).OfType<TestPropertyAttribute>().FirstOrDefault(); //get the instance of TestPropertyAttribute of "MyTestMethod"
            Console.WriteLine(myAttribute.Name);  //output the name:VariantName
            Console.WriteLine(myAttribute.Value); //output the value:1
        }
    }

注意:此方法是获取TestProperty的实例,因此我们可以使用它来获取名称和相应的值在一起。

Note: This method is to get the instance of TestProperty, so we can use it to get name and corresponding value together.

此外:您可以从
此问题
 关于获取C#中的属性。

In addition: You can get more info from this issue about get attribute in C#.

关于specflow的详细方案,似乎第三方扩展,我不熟悉它。很抱歉给您带来不便。

And as for the detailed scenario about specflow, it's seems a third-party extension and I'm not familiar with it. Sorry for this inconvenience.

希望以上所有内容都有所帮助,如果有任何更新,请随时在此分享。

Hope all above can help, if there has any update, feel free to share here.

最好的问候

Lance


这篇关于如何在运行时获取TestPropertyAttribute的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆