2

I am using CodedUI for Automation Testing. As I am using two Applications suppose A and B. A needs to be started at the start of execution of each test case. So i have put it in TestInitialize. So i call a function from TestInitialize and send the parameter i.e. location of the Application A. But what if there are different version of App a And i want to send a parameter to TestInitialize .

My present Code is

[TestInitialize()] public void MyTestInitialize() { App_Launch(@"C:\Emulator\App_Version1\Launch_App.exe"); } 

There are diff app versions So if i have to start App_Version2 then i have to change the folder name in the parameter which is hard coded.

What if i Want the following

[TestInitialize()] public void MyTestInitialize(string Path) { App_Launch(@Path); } 

send the path to TestInitialize

2
  • Okay i did that. But now how to send the parameters be default? how to call TestInitialize with the path Commented Mar 19, 2015 at 6:40
  • with the xUnit test framework you can use [Theory] for this Commented Mar 19, 2015 at 6:41

1 Answer 1

11

You cannot add a parameter to a method marked with the TestInitializeAttribute, but there are some alternatives.

  1. If you have a number of tests for version 1 and a number for version 2, the best option (imo) would be to simply create two different test classes.

  2. You could simply not use [TestInitialize] and call one of two private methods at the beginning of each test, each starting the respective application.

  3. You could use a TestContext property in some way. It's set automatically prior to the test being executed and can be accessed from the TestInitialize method. The information therein may give you enough information to decide which application to start. I'm not sure there is ever a good reason to pick this over option 1 though.

  4. If all in tests needs to be performed on both versions, you could use a data driven test. This allows you to execute a single test multiple times with different input. This also requires the TestContext attribute. However, personally I find data driven tests in MSTest horrendous. It requires the use of the DataSourceAttribute, unless you're targeting Windows Store applications.

  5. Use inheritance. Put all the tests that need to execute for both versions in a base class without a [TestInitialize]. Put only the test initialization in the two inherited classes. Do not mark the base class with the TestClassAttribute and preferrably make it abstract so the tests won't also be run without any form of initialization.

It all depends on the situation, really. If you feel you really need option 4 and you're not targeting Windows Store, I would seriously consider NUnit, which offers much more flexible parameterized tests.

Sign up to request clarification or add additional context in comments.

1 Comment

Can I add a Data Source to TestInitialize Method using Coded UI DataSourceAttribute? Actually, I tried the same but variables defined in TestInitialize method are picking values from the Data Source of the Test Method, while I want them to pick values from Data Source of TestInitialize. In short, I want to have separate data source for TestInitialize method, is it possible?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.