Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MsTest, DataSourceAttribute - how to get it working with a runtime generated file?

for some test I need to run a data driven test with a configuration that is generated (via reflection) in the ClassInitialize method (by using reflection). I tried out everything, but I just can not get the data source properly set up.

The test takes a list of classes in a csv file (one line per class) and then will test that the mappings to the database work out well (i.e. try to get one item from the database for every entity, which will throw an exception when the table structure does not match).

The testmethod is:

[DataSource(
    "Microsoft.VisualStudio.TestTools.DataSource.CSV",
    "|DataDirectory|\\EntityMappingsTests.Types.csv",
    "EntityMappingsTests.Types#csv",
    DataAccessMethod.Sequential)
]
[TestMethod()]
public void TestMappings () {

Obviously the file is EntityMappingsTests.Types.csv. It should be in the DataDirectory.

Now, in the Initialize method (marked with ClassInitialize) I put that together and then try to write it.

WHERE should I write it to? WHERE IS THE DataDirectory?

I tried:

File.WriteAllText(context.TestDeploymentDir + "\\EntityMappingsTests.Types.csv", types.ToString());
File.WriteAllText("EntityMappingsTests.Types.csv", types.ToString());

Both result in "the unit test adapter failed to connect to the data source or read the data". More exact:

Error details: The Microsoft Jet database engine could not find the object 'EntityMappingsTests.Types.csv'. Make sure the object exists and that you spell its name and the path name correctly.

So where should I put that file?

I also tried just writing it to the current directory and taking out the DataDirectory part - same result. Sadly, there is limited debugging support here.

like image 902
TomTom Avatar asked Sep 22 '11 04:09

TomTom


2 Answers

Please use the ProcessMonitor tool from technet.microsoft.com/en-us/sysinternals/bb896645. Put a filter on MSTest.exe or the associate qtagent32.exe and find out what locations it is trying to load from and at what point in time in the test loading process. Then please provide an update on those details here .

like image 138
kroonwijk Avatar answered Nov 01 '22 05:11

kroonwijk


After you add the CSV file to your VS project, you need to open the properties for it. Set the Property "Copy To Output Directory" to "Copy Always". The DataDirectory defaults to the location of the compiled executable, which runs from the output directory so it will find it there.

like image 41
Rn222 Avatar answered Nov 01 '22 03:11

Rn222