68171

Forcing ClassInitialize to execute before testmethod data is read

Question:

I'm having a problem with the classinitilalize method of a unit test being executed after the evaluation of the attributes on a unit test. These tests use a [classinitialize] method to generate a test set that makes a random set of data in an xml file. The xml file is consumed by a unit test with the attribute [datasource]. A trimmed down version of this looks like this:

[ClassInitialize] public static void Initialize(TestContext context) { // Create xml output file var output = new XDocument(new XElement("Rows")); foreach (var Code in Codes) { if (output.Root != null) output.Root.Add(new XElement("Row", new XElement("Code", Code))); } output.Save("TestData.xml"); } [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\TestData.xml", "Row", DataAccessMethod.Sequential)] [TestMethod] public void TestStuff() { var Code = (string)TestContext.DataRow["Code"]; TestItem(Code); }

I'm working with Visual Studio 2012 Update 3. I have used the Sysinternals tool Process Monitor to verify the order of execution. I set a break point at the beginning of each method. I then stepped through and watched the file access. The order of operations I saw.

<ol><li>TestData.xml read by vstestexecutionengine.</li> <li>ClassInitialize Method is called and writes TestData.xml</li> <li>TestMethod is executed, using data read in step 1. (I added a file with bogus data to test)</li> </ol>

So my question is, what am I doing wrong that classinitialize is not executing before the xml file is read? Or is what I am trying to do impossible?

Answer1:

<a href="https://github.com/Thwaitesy/MSTestHacks" rel="nofollow">MSTestHacks</a> might be a possible workaround for you.

It allows an IEnumberable on your test class to be used as the DataSource for your TestMethod. You can return what you have as Codes in this property.

From the website:

<h2>Runtime DataSource</h2>

You MUST inherit your test class from TestBase

[TestClass] public class UnitTest1 : TestBase { }

Create a Property, Field or Method, that returns an IEnumerable

[TestClass] public class UnitTest1 : TestBase { private IEnumerable<int> Stuff { get { //This could do anything, fetch a dynamic list from anywhere.... return new List<int> { 1, 2, 3 }; } } }

Add the DataSource attribute to your test method, pointing back to the IEnumerable name created earlier. This needs to be fully qualified.

[TestMethod] [DataSource("Namespace.UnitTest1.Stuff")] public void TestMethod1() { var number = this.TestContext.GetRuntimeDataSourceObject<int>(); Assert.IsNotNull(number); }

Recommend

  • How can I implement my own async callback in C# wp7 silverlight?
  • How can I make the namespace locally match what is listed on MSDN?
  • saving model output from Decision tree train classifier as a text file in Spark Scala platform
  • Put quotes around each word in comma separated sql string?
  • android webview overflow cannot be disabled
  • Reading files from Apache Spark textFileStream
  • How to run xUnit 2.1.0-beta-* for DNX projects with ReSharper
  • What components I need to create VS 2017 offline layout for UWP development?
  • Vue props data not updating in child component
  • Git cleanup/garbage collection on remote VSO git repository
  • Is is safe to use HSQLDB for production? (JBoss AS5.1)
  • Is there a way to disable a specific event in kendo ui scheduler?
  • Xamarin MonoAndroid Azure mobile service InsertAsync
  • XSLT foreach repeating nodes to flat
  • C#: Import/Export Settings into/from a File
  • Private IP address in reserved subnet range
  • How To Customize ASP.NET Chart Databound To SqlDataSource
  • Row to Column conversion in Talend
  • Merging rows to columns
  • Low TTL with Leveled Compaction, should I reduce gc_grace_seconds to improve read performance withou
  • List images(01.png) and descriptions(01.txt) from directory
  • How can I set a binding to a Combox in a UserControl?
  • AJAX Html Editor Extender upload image appearing blank
  • Limiting recursion to certain level - Duplicate rows
  • Word Open XML Mail Merge
  • How do I alternate colors in Flat List (React Native)
  • D3 nodes and links from JSON with nested arrays of children
  • Retrieving value from sql ExecuteScalar()
  • vba code to select only visible cells in specific column except heading
  • In LanguageTool, how do you create a dictionary and use it for spell checking?
  • Do I've to free mysql result after storing it?
  • Unanticipated behavior
  • Transpose CSV data with awk (pivot transformation)
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • Can't mass-assign protected attributes when import data from csv file
  • Sorting a 2D array using the second column C++
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app