1
votes

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?

2
Has the CSV file been deployed to the TestContext.DeploymentDirectory directory as wanted by the |DataDirectory| part of the DataSource attribute. You should examine the contents of TestContext.DeploymentDirectory in both the successful and the unsuccessful runs. - AdrianHHH

2 Answers

0
votes

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

0
votes

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"