代码之家  ›  专栏  ›  技术社区  ›  Mike ASP

如何更改在BaseTest-set-up方法下调用的类的属性值

  •  1
  • Mike ASP  · 技术社区  · 6 年前

    资源:c#、Nuniut、Selenium、VS 2017、restSharp

    1) 这是我的测试类,继承的BaseTest类。

    [TestFixture]
    public class ABCTest : BaseTest
    {
         [Test]
        [Retry(Constant.IterationRunsInCaseFailure)]
        public void ABC_Output()
        {            
            Common.ExecuteTest(ABC.CheckAdd, GetType().Name, MethodName);
        }
    }
    

    2) 这是基类,将在任何测试之前首先执行。

    [TestFixture]
    public class BaseTest
    {
        [SetUp]
        public void Init()
        {
            Driver.ConfigInit();
            if (Driver.BaseAddress.Contains("dev.com"))
            {                           
            LoginPage.Login();
            }
            else
            {               
                Assert.Fail("Please check URL  ");
            }
    
    // I am calling this "TokenGenerate" method to get token and other stuff.
    //since it's defined under "set up" method I am not sure how to change the property values.
    
            string url = TokenRequest.TokenGenerate();
            Driver.Instance.Navigate().GoToUrl(url);
    
         }
    }
    

    3) 这是我感兴趣的“TokenGenerate”方法下的“TokenRequest”类。

    public class TokenRequest
    {
        public static string TokenGenerate()
        {
            var client = new RestClient("any url ");
            var request = new RestRequest(Method.POST);
    
            //  I want change the value of this PostMe properties for few test cases. since it's a executed under"BaseTest" and called before any test so I am not sure how to change these properties "Name, ProxyUrl etc" according to test. 
    
            var postMe = new PostMe()
            {
                Name = "ABC ABC",
                ManagementId = "ABC ABC",                
                ProxyUrl = ABC,
                SourceFilename = ABC,
            };
         }
    }
    

    4) 这是要运行的测试的实际实现,在这里可以避免。 我有这样的测试用例,它们都使用PostMe属性的默认值,但我想更改此测试用例的值。

    public class ABC
    {       
        public static bool CheckAdd()
        {
            CommonOutput.OpenMediaAndClickCheckbox(Constant.ABC);           
            return true;            
        }
    }    
    

    this is expected value for any test :
    PostMe class is define with these properties separately in the project.
    
    var postMe = new PostMe()
                    {
                        Name = "ABC ABC",
                        ManagementId = "ABC ABC",                
                        ProxyUrl = ABC,
                        SourceFilename = ABC,
                    };
    
    but what I want for few test cases is :
    
    var postMe = new PostMe()
                    {
                        Name = "ABCDEF ABCDEF ABCDEF ABCDEF  ",
                        ManagementId = "ABCDEF ABCDEF ABCDEF  ",                
                        ProxyUrl = ABCDEF  ABCDEF ABCDEF ,
                        SourceFilename = ABCDEF  ABCDEF ABCDEF ,
                    };
    

    请让我知道解决方案或不同类型的实现。

    1 回复  |  直到 6 年前
        1
  •  1
  •   andreasnico    6 年前

    将TokenRequst更改为:

    public class TokenRequest
        {
            public PostMe PostMe { get; set; }
    
            public TokenRequest()
            {
                PostMe = new PostMe()
                {
                    Name = "ABC ABC",
                    ManagementId = "ABC ABC",
                    ProxyUrl = ABC,
                    SourceFilename = ABC,
                }; ;
            }
    
            public TokenRequest(PostMe postMe)
            {
                PostMe = postMe;
            }
    
            public static string TokenGenerate()
            {
                var client = new RestClient("any url ");
                var request = new RestRequest(Method.POST);
    
                //  I want change the value of this PostMe properties for few test cases. since it's a executed under"BaseTest" and called before any test so I am not sure how to change these properties "Name, ProxyUrl etc" according to test. 
    
                var postMe = PostMe;
            }
        }