2

I have an issue related to running CodedUITests from a Windows Form. (it works in the CodedUITestProject with Test-Explorer).

This is my CodedUITest's structure :

 [CodedUITest]
    public class DBTest
    {
        #region Static Fields
        public static CSVReader csvReader;
        #endregion

        #region Fields
        public string logFileName;
        public string timer = String.Empty;
        public  TestRead testRead;
        public  UploadResults uploadResults;
        private DateTime testStart;
        #endregion

   [ClassInitialize]
    public static void MyTestInitialize(TestContext test)
    {

        csvReader = new CSVReader();
        csvReader.LoadTestValues("steam.csv");

    }

    [TestInitialize]
    public void testInit()
    {

        testStart = DateTime.Now;
    }

    [TestMethod(), TestCategory("Reflection"), TestCategory("DataDriven"), DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", @"|DataDirectory|\CSV's\steam.csv", "steam#csv", DataAccessMethod.Sequential), DeploymentItem(@"..\..\CSV's\steam.csv")]
    public void steamAccess()
    {
        testRead = new TestRead();
        SteamMap a = new SteamMap();

        testRead.Read(a, TestContext);

    }  

    [TestCleanup]
    public void TestCleanup()
    {
        uploadResults = new UploadResults();
        //timer for each test ( in seconds )
        double diffSecs = (DateTime.Now - testStart).TotalSeconds;

        uploadResults.TestUpload(testRead.TestResults, csvReader.DataTable, diffSecs,TestContext);
    }

    public  TestContext TestContext
    {
        get
        {
            return testContextInstance;
        }
        set
        {
            testContextInstance = value;
        }
    }
    private   TestContext testContextInstance;
}

This runs perfectly from VS's Test Explorer, testContextInitialize variable gets initialized. In the same Solution I've added a second project, a simple Windows Form application, added a reference to my DLL ( from References) which runs the following code :

            Playback.Initialize();
            DBTest a = new DBTest();
            a.steamAccess();
            Playback.Cleanup();

NullReferenceException occurs, my testContex is null when I run my test from outside it's assembly. I need some help in this matter, Thanks

Edit 1: Test Class :

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Input;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;
using System.Linq;
using System.Reflection;
using SimarpiDB.GLobal;
using SimarpiDB.UITestMaps.SteamMapClasses;
using SimarpiDB.Global;
using System.Data;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("TestLauncher")]

namespace SimarpiDB
{

    [CodedUITest]
    public class.....

enter image description here

Edit 2: Temporary workaround until I find the cause of this error : From my Windows Form I launch a MSTest with few required parameters and most of the what-methods-to-run logic will come from my database. I already did this few months ago but I scraped it because it's an additional performance overhead to use a tool such as MsTest within a launcher such as mine. For anyone interested, there's a file located within VS's installation directory, it's called VsDevCmd.bat, I load this .bat within a hidden-in-background cmd with few additional commands ( mstest, testcontainer, test). This works but as I said I have no other plausible ideas. There may also be a lack of referenced libraries within my Form ? Maybe something, a .dll that initialized the testenvironment and the testContext variable. I wrote this because there may be others seeking the same result.

ExtremeSwat
  • 794
  • 1
  • 12
  • 34
  • Look here: http://stackoverflow.com/questions/15440935/how-to-test-internal-class-library , or make testContex class public – Mattias May 03 '15 at 07:07
  • I might be wrong but the TesContext class is declared as Public Abstract within it's own definitions. I've tried as you suggested with [assembly:InternalsVisibleTo("MyProjName")] without much success. Also my testContext is already declared public. – ExtremeSwat May 03 '15 at 07:31
  • possible duplicate of [Executing coded UI test from a standalone application](http://stackoverflow.com/questions/13482796/executing-coded-ui-test-from-a-standalone-application) – AdrianHHH May 03 '15 at 07:44
  • Coded UI Tests are intended to be run by software written and supplied my Microsoft. As far as I know they have no documented the exact requirements, but they do supply programs like `mstest` also the *agents and controllers*. – AdrianHHH May 03 '15 at 07:46
  • try adding references to all the dll's that are needed to run codedui tests to the winforms project – barakcaf May 04 '15 at 12:00
  • +barakcaf unfortunately that isn't working either, that darn testContext won't initialize itself. I'll try with setting it as as static variable, maybe that'll work. – ExtremeSwat May 08 '15 at 09:09
  • I will have to test the possibility of not using tags within my WinForm, tags such as [CodedUITest] or [TestMehtod()) , by applying these identifiers to my methods I may be able to initialize the testContext variable. – ExtremeSwat May 08 '15 at 09:11

1 Answers1

1

To clarify on my comment: internal is default in C#, so declaring something as abstract is like declaring it as "internal abstract".

For InternalsVisibleTo, you must make the library project visible to the test project, not the other way around: [assembly:InternalsVisibleTo("MyTestProject")]

Mattias
  • 1,110
  • 1
  • 11
  • 26