Programming‎ > ‎

MSTEST

MSTEST

If you have Visual Studio Team Edition or Visual Studio Professional with Team Foundation Server, then you have "Test" menu at the menubar.  I have not done much with this option in the past.   Here is my learnings.

Index


How can I enable Code Coverage?

Here is the steps you have to do:

  
  1. Right click Local.Testsettings and clock Open.  
  2. Click on "Data and Diagnostics" on the left pane in Test Settings dialog.  
  3. Check "Code Coverage" on the lower right.    
  4. Then Configure is enabled.  Double click it.  
  5. Select dll you want to have Code Coverage measured in      
     "Select artifacts to instrument." and OK.  
  6. Now you have results.  Open Code Coverage Results window.
  

Top


How can I test a MEFed class?

It turned out that there is a very simple way to modify Visual Studio Unit Test Wizard generated unit tests for a MEFed class. Get the document attached on this page. The trick is to add a static MEFed class object to a test class and the dependency injection will assign the value by container.GetExportedValue.

Top


How can I have data files in a subdirectory for test?

My unit test were the ESRI Python module tests which needs Python scripts files. If you just copy scripts to where the test executable lives, then just use Deployment Item dialog by double clicking Local.testsettings in Solution Item and push "Add Directory ..." button. You have to close the solution and reopen to make this deployment available. All files in the directory is copied into "Out" directory of the TestResults incidence directory. If you need to preserve the directory, you have to specify outputDirectory in Local.testsettings. This task cannot be done in the dialog. You right click Local.testsettings and select "Open with ..." and pick "XML (Text) Editor". Search for <Deployment> section and edit the relevant lineto add outputDirectory:

 
  <DeploymentItem filename="(directory)\" outputDirectory="Python scripts" >
  

The (directory) is the actual directory path like "D:\My scripts". Remember the last back slash to note that this is the directory. You can specify the relative path with respect to also. Note that the outputDirectory is relative and thus this example will copy all files in (directory) into executable subdirectory called "Python scripts".

Another way is to use the attribute DeploymentItem. In this case, the relative path is with respect to the project dir:

  
  [DeploymentItem("Data\", "Data\")]
  

All files in Data directory relative to the project directory will be copied into Test Output subdirectory named Data. This attribute can be used for the test class (MSDN says otherwise) or test method. There is a catch. If you use the attribute, you have to make sure that all files must have Property to be Copy (copy always or copy if new). If you don't set the property, files are not copied. This Property setting is not needed if you use local.testsettings.

Another trick when your data files are not in the project directory. You cannot use the deployment attribute, since you can use the relative path from the project directory. In this case you Add a new folder in Visual Studio and add existing items dialog and use Add Links. You have to use the small down arrow to pick this option. Otherwise you create copies of files.

Top


How can I increase the number of test runs to keep?

While running the tests, I go the following message box.

As it says, you should open Tools → Options and change the setting here:

Top


Can you list attributes and asserts usable under Test?

Look at the this file.

Top


How can I create an ordered test?

I wanted to order how the tests generated by Visual Studio. There is no way to specify in the unit tests. It turned out that you create an ordered test from existing tests.

   
  1. Test → New Test.  You get a dialog.   
  2. You create unit tests using wizard.  
  3. This will produce unit tests.      
     (If you run these, the order of run is not the way the test code is written.)   
  4. Test → New Test.  Pick Ordered Test as template.   
  5. Pick the tests and arrange the order. Right click on the page tab to save it.   
  6. Create a New Test List by Test → Windows → Test List Editor. Group by TestType.   
  7. Add an ordered test and check it.   
  8. The tool bar in Test List Editor has Run checked tests.
  

Top


How can I run a specific test by command line

Inside the Visual Studio, it is easy to run a specific test by checking the test. If you want to run a particular test from a command line, here is the way

  
  1. Start the Visual Studio command line.  
  2. Go to the solution directory.  
  3. mstest /testmetadata:(name).vsmdi /testlist:(particulartestname)
  

Note that the file vsmdi is an XML text file so that you can see what is inside.

Top


How can I resolve a DLL not trusted

(Editing this item now....) When I try to run a unit test which used DotNetZip Ionic.Zip.dll, mstest complained that Ionic.Zip.dll is not trusted. Here is the error message under Visual Studio 2008:

  
  Failed to queue test run:  Test Run deployment issue:  
  The location of the file or directory '(filename)' is not trusted.
  
Some people say that .NET Framework 2.0 Configuration will fix. In my case, only caspol fixed the problem. Note that this solution is only for a local developer machine.  I do not recommend to use this, since one typo causes a wrong behavior.  Note that .NET Framework 2.0 Configuration is a GUI and thus when you make a mistake, you can delete it.  Unfortunately, Microsoft removed it from distribution. You can still find it here at
 
The better solution I found for the temporary workaround with MSTEST (until you get your own Authenticode) is to add Strong Name to the dll, even though this is not what Microsoft recommends. Microsoft is now demands only the Authenticode signed (digital signature) assemblies and Strong Name checking is dropped from .NET v3.5SP1.

Top


Updated 8/1/2012

Home

Subpages (1):testmembers
Yasunari Tosa,
Jul 7, 2012, 9:08 PM
v.1