More VSTS Unit Testing learnings

I was having problems getting a very simple test to work this morning using VSTT, and I distilled my problem down to its most simple form:

using System;
using Microsoft.VisualStudio.QualityTools.UnitTesting.Framework;
using System.IO;

namespace FileCopyingBug
{
    [TestClass]
    public class FileCopyFixture
    {
        [TestMethod]
        public void WillCopyFileWhenRun()
        {
            Assert.IsTrue(File.Exists("TestFile.txt"));
        }
    }
}

And I created the external TestFile.txt in my project directory and set its properties to have it copied to the output directory always.

Depending on how I chose to run this test, it would either work or fail. If I ran it using the Test View pane in VSTT and I chose “Run” or “Debug” the test would fail. If I ran it through TestDriven.Net, it would work if I ran the test, but fail if I debugged the test. And if I translated the test to NUnit, it would work through TestDriven.Net all the time. Very interesting.

It would seem that, according to the Principal of Least Surprise, this test should just work.

But it turns out that VSTT copies your executable and its dependencies to another directory whenever it runs your tests, so just because you made sure some files showed up in your binDebug directory doesn’t actually mean that they showed up where your program was being executed from. This seems a bit fishy to me, but at least I know about it now.

The Fix — Deployment Items

Many thanks to Tom Arnold, the PM for the unit testing tools in VSTS, and the people who work with him for giving me the answer to this very quickly. I really do appreciate it!

There is an attribute that you can apply to your TestMethods called the DeploymentItemAttribute. If you apply this attribute and give it the name of the file you want copied, it will make sure that it copies the named file to the real output directory. Once I did this, my test worked just fine.

You can also apply this attribute to your TestClasses, but one web site I saw said that there was a bug in this. If you ran your tests through the IDE, it would not respect this attribute. If you ran them from the command line using MSTest.exe, it would work fine. I did not verify this, so it may apply to an earlier verion, etc.

Finally, you can also make this fix by editing the Run Configuration for your test. If you open the Test menu and choose Edit Test Run Configurations, a dialog box pops up. On this dialog, you can detail which files you would like treated as deployment items. I fixed my problem like this, and once I did, everything worked great.

Conclusion

Once again, I really do feel that these tests should just work. The fact that VSTT is doing magic behind the scenes for whatever personal reasons shouldn’t matter to me, as that was an implementation decision that they made. Fortunately there is this very simple fix for it, however, and I wanted to write about it so I could remember it a month from now when it bit me again

— bab

 

11 thoughts to “More VSTS Unit Testing learnings”

  1. I tried DeploymentItemAttribute and my file won’t get copied. Can you post some sample?

    Must the source file be in the output directory? What is the purpose of the DeploymentItemAttribyte ctor with an output directory param?

  2. On this dialog, you can detail which files you would like treated as deployment items. I fixed my problem like this, and once I did, everything worked great.?

  3. http://my.opera.com/opdy05/homes/blog/sanjip.htm

    http://my.opera.com/opdy05/homes/blog/huangsedy.htm

    http://my.opera.com/opdy05/homes/blog/seqingdy.htm

    http://my.opera.com/opdy05/homes/blog/jiqingxdy.htm

    http://my.opera.com/opdy05/homes/blog/chengrendy.htm

    http://my.opera.com/opdy05/homes/blog/xingaidy.htm

    http://my.opera.com/opdy05/homes/blog/xiezhen.htm

    http://my.opera.com/opdy05/homes/blog/zouguang.htm

    http://my.opera.com/opdy05/homes/blog/zipai.htm

    http://kan106.googlepages.com/200601.htm

    http://kan106.googlepages.com/200602.htm

    http://kan106.googlepages.com/200603.htm

    http://kan106.googlepages.com/200604.htm

    http://kan106.googlepages.com/200605.htm

    http://kan106.googlepages.com/200606.htm

    http://kan106.googlepages.com/200607.htm

    http://kan106.googlepages.com/200608.htm

    http://kan106.googlepages.com/200609.htm

    http://kan106.googlepages.com/200610.htm

    http://kan106.googlepages.com/200611.htm

    http://kan106.googlepages.com/200612.htm

    http://kan106.googlepages.com/200613.htm

    http://kan106.googlepages.com/200614.htm

    http://kan106.googlepages.com/200615.htm

    http://kan106.googlepages.com/200616.htm

    http://kan106.googlepages.com/200617.htm

    http://kan106.googlepages.com/200618.htm

    http://kan106.googlepages.com/200619.htm

    http://kan106.googlepages.com/200620.htm

    http://kan106.googlepages.com/200621.htm

    http://kan106.googlepages.com/200622.htm

    http://kan106.googlepages.com/200623.htm

    http://kan106.googlepages.com/200624.htm

    http://kan106.googlepages.com/200625.htm

    http://kan106.googlepages.com/200626.htm

  4. http://my.opera.com/007smsdy/homes/files/2541.htm

    http://my.opera.com/007smsdy/homes/files/2540.htm

    http://my.opera.com/007smsdy/homes/files/2539.htm

    http://my.opera.com/007smsdy/homes/files/2538.htm

    http://my.opera.com/007smsdy/homes/files/2537.htm

    http://my.opera.com/007smsdy/homes/files/2536.htm

    http://my.opera.com/007smsdy/homes/files/2535.htm

    http://my.opera.com/007smsdy/homes/files/2534.htm

    http://my.opera.com/007smsdy/homes/files/2533.htm

    http://my.opera.com/007smsdy/homes/files/2532.htm

    http://my.opera.com/007smsdy/homes/files/2531.htm

    http://my.opera.com/007smsdy/homes/files/2530.htm

    http://my.opera.com/007smsdy/homes/files/2529.htm

    http://my.opera.com/007smsdy/homes/files/2528.htm

    http://my.opera.com/007smsdy/homes/files/2527.htm

    http://my.opera.com/007smsdy/homes/files/2526.htm

    http://my.opera.com/007smsdy/homes/files/2525.htm

    http://my.opera.com/007smsdy/homes/files/2524.htm

    http://my.opera.com/007smsdy/homes/files/2523.htm

    http://my.opera.com/007smsdy/homes/files/2522.htm

    http://my.opera.com/007smsdy/homes/files/2521.htm

    http://my.opera.com/007smsdy/homes/files/2520.htm

    http://my.opera.com/007smsdy/homes/files/2519.htm

    http://my.opera.com/007smsdy/homes/files/2518.htm

    http://my.opera.com/007smsdy/homes/files/2517.htm

    http://my.opera.com/007smsdy/homes/files/2516.htm

    http://my.opera.com/007smsdy/homes/files/2515.htm

    http://my.opera.com/007smsdy/homes/files/2514.htm

    http://my.opera.com/007smsdy/homes/files/2513.htm

    http://my.opera.com/007smsdy/homes/files/2512.htm

    http://my.opera.com/007smsdy/homes/files/2511.htm

    http://my.opera.com/007smsdy/homes/files/2510.htm

    http://my.opera.com/007smsdy/homes/files/2509.htm

    http://my.opera.com/007smsdy/homes/files/2508.htm

    http://my.opera.com/007smsdy/homes/files/2507.htm

    http://my.opera.com/007smsdy/homes/files/2506.htm

    http://www.greatestjournal.com/users/kan201/316.html

    http://www.greatestjournal.com/users/kan201/726.html

    http://www.greatestjournal.com/users/kan201/826.html

    http://www.greatestjournal.com/users/kan201/1048.html

    http://www.greatestjournal.com/users/kan201/1436.html

    http://www.greatestjournal.com/users/kan201/1645.html

    http://www.greatestjournal.com/users/kan201/1991.html

    http://www.greatestjournal.com/users/kan201/2211.html

    http://www.greatestjournal.com/users/kan201/2409.html

    http://www.greatestjournal.com/users/kan201/2750.html

    http://www.greatestjournal.com/users/kan201/3028.html

    http://www.greatestjournal.com/users/kan201/3177.html

    http://www.greatestjournal.com/users/kan201/3427.html

    http://www.greatestjournal.com/users/007sms/732.html

    http://www.greatestjournal.com/users/007sms/446.html

    http://www.greatestjournal.com/users/007sms/789.html

    http://www.greatestjournal.com/users/007sms/1076.html

    http://www.greatestjournal.com/users/007sms/1308.html

    http://www.greatestjournal.com/users/007sms/1600.html

    http://www.greatestjournal.com/users/007sms/1857.html

    http://www.greatestjournal.com/users/007sms/2268.html

    http://www.greatestjournal.com/users/007sms/2515.html

    http://www.greatestjournal.com/users/007sms/2735.html

    http://www.greatestjournal.com/users/007sms/3044.html

    http://www.greatestjournal.com/users/007sms/3194.html

    http://www.greatestjournal.com/users/007sms/3377.html

    http://www.greatestjournal.com/users/007sms/3660.html

    http://www.greatestjournal.com/users/007sms/3891.html

    http://www.greatestjournal.com/users/007sms/4248.html

    http://www.greatestjournal.com/users/007sms/4553.html

    http://www.greatestjournal.com/users/007sms/4629.html

    http://www.greatestjournal.com/users/007sms/5022.html

    http://www.greatestjournal.com/users/007sms/5220.html

    http://www.greatestjournal.com/users/007sms/5608.html

  5. http://www.greatestjournal.com/users/ppgirl3/605.html

    http://www.greatestjournal.com/users/ppgirl3/438.html

    http://www.greatestjournal.com/users/ppgirl3/962.html

    http://www.greatestjournal.com/users/ppgirl3/1151.html

    http://www.greatestjournal.com/users/ppgirl3/1306.html

    http://www.greatestjournal.com/users/ppgirl3/1635.html

    http://www.greatestjournal.com/users/ppgirl3/1800.html

    http://www.greatestjournal.com/users/ppgirl3/2073.html

    http://www.greatestjournal.com/users/ppgirl3/2330.html

    http://www.greatestjournal.com/users/ppgirl3/2695.html

    http://www.greatestjournal.com/users/ppgirl3/2879.html

    http://www.greatestjournal.com/users/ppgirl3/3109.html

    http://www.greatestjournal.com/users/ppgirl3/3556.html

    http://www.greatestjournal.com/users/ppgirl3/3744.html

    http://www.greatestjournal.com/users/ppgirl3/4033.html

    http://www.greatestjournal.com/users/ppgirl3/4218.html

    http://www.greatestjournal.com/users/ppgirl3/4560.html

    http://www.greatestjournal.com/users/ppgirl3/4628.html

    http://www.greatestjournal.com/users/ppgirl3/5075.html

    http://www.greatestjournal.com/users/ppgirl3/5293.html

    http://www.greatestjournal.com/users/ppgirl3/5488.html

    http://www.greatestjournal.com/users/ppgirl3/5740.html

    http://www.greatestjournal.com/users/ppgirl3/5955.html

    http://www.greatestjournal.com/users/ppgirl3/6334.html

    http://my.opera.com/007smsdy/homes/files/2541.htm

    http://my.opera.com/007smsdy/homes/files/2540.htm

    http://my.opera.com/007smsdy/homes/files/2539.htm

    http://my.opera.com/007smsdy/homes/files/2538.htm

    http://my.opera.com/007smsdy/homes/files/2537.htm

    http://my.opera.com/007smsdy/homes/files/2536.htm

    http://my.opera.com/007smsdy/homes/files/2535.htm

    http://my.opera.com/007smsdy/homes/files/2534.htm

    http://my.opera.com/007smsdy/homes/files/2533.htm

    http://my.opera.com/007smsdy/homes/files/2532.htm

    http://my.opera.com/007smsdy/homes/files/2531.htm

    http://my.opera.com/007smsdy/homes/files/2530.htm

    http://my.opera.com/007smsdy/homes/files/2529.htm

    http://my.opera.com/007smsdy/homes/files/2528.htm

    http://my.opera.com/007smsdy/homes/files/2527.htm

    http://my.opera.com/007smsdy/homes/files/2526.htm

    http://my.opera.com/007smsdy/homes/files/2525.htm

    http://my.opera.com/007smsdy/homes/files/2524.htm

    http://my.opera.com/007smsdy/homes/files/2523.htm

    http://my.opera.com/007smsdy/homes/files/2522.htm

    http://my.opera.com/007smsdy/homes/files/2521.htm

    http://my.opera.com/007smsdy/homes/files/2520.htm

    http://my.opera.com/007smsdy/homes/files/2519.htm

    http://my.opera.com/007smsdy/homes/files/2518.htm

    http://my.opera.com/007smsdy/homes/files/2517.htm

    http://my.opera.com/007smsdy/homes/files/2516.htm

    http://my.opera.com/007smsdy/homes/files/2515.htm

    http://my.opera.com/007smsdy/homes/files/2514.htm

    http://my.opera.com/007smsdy/homes/files/2513.htm

    http://my.opera.com/007smsdy/homes/files/2512.htm

    http://my.opera.com/007smsdy/homes/files/2511.htm

    http://my.opera.com/007smsdy/homes/files/2510.htm

    http://my.opera.com/007smsdy/homes/files/2509.htm

    http://my.opera.com/007smsdy/homes/files/2508.htm

    http://my.opera.com/007smsdy/homes/files/2507.htm

    http://my.opera.com/007smsdy/homes/files/2506.htm

Leave a Reply to TRDGCF Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.