简体   繁体   中英

Run data driven coded ui from console app

I have a requirement to run Coded UI test that is data driven from console application. If I run the Coded UI test as a standalone, then [DataSource] can access the values from the CSV file. Whereas if I call Coded UI from the console app, I get the unhandled exception: System.NullReferenceException: Object reference not set to an instance of an object. - since TestContext.DataRow is null.

Here's snippet from the code

Program.cs (console app):

public class Program
{
    static void Main(string[] args)
    {
        Playback.Initialize();
        CodedUITestWarmup test = new CodedUITestWarmup();
        test.WarmUp();
        Playback.Cleanup();
    }
}

CodedUITestWarmup.cs (coded ui test):

public class CodedUITestWarmup
{
    [TestMethod]    
    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", @"|DataDirectory|\DataFiles\warmup.csv", "warmup#csv", DataAccessMethod.Sequential)] 
    public void WarmUp()
    {
        InitializeVendorTest();
        ...
    } 

    private void InitializeVendorTest()
    {      
        caseV = new CaseVariables(TestContext);
        ...
    }      
}

class CaseVariables
{
    public string lastNameID;
    ...

    public CaseVariables(TestContext TestContext)
    {
        lastNameID = TestContext.DataRow["lastNameID"].ToString();
         ...
    }
}

Could you please provide some inputs what can be done in this regard?

You cannot run codded ui test without vstestconsole.exe or from visual studio.

You can try run vstestconsole.exe with parameter (path to your test dll) eg. C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\Common7\\IDE\\CommonExtensions\\Microsoft\\TestWindow\\vstestconsole.exe

Dll file will be created during each build the codedui test project

Why do you need the test to run from a console application? I believe you can use either vstestconsole.exe or mstest.exe

vstestconsole is command line tool that replaces MStest. But in this case I thing any of them can do wat you want!

A Coded UI Test or Unit Test will allwais need to have the TestContext initialized, and the test engine is responsible to do that, that is why you get an exception.

In my PC the mstest executable is in "c:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\Common7\\IDE\\" So I can start the command prompt, navigate to the mstest.exe folder (the one abouve) and use a command like the following one:

mstest.exe /testcontainer:"c:/TestFolder/testassembly.dll" /test:"TestNamespace.MyTestToExecute_TestMethod1"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM