locked
Using vs2010 unit test, I want to read binary file in my test RRS feed

  • Question

  • Any suggestion on how to read a binary file to be used with a unit test?  That is, I have a file called mytestdata.dat, I want to open it in the test, then do somw work on it.

    Thanks,


    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider
    Friday, August 27, 2010 9:25 PM

Answers

All replies

  • Hi,

    Reading a file in unit test is no different than reading a file in any C# project using BinaryReader. You can refer http://dotnetperls.com/binaryreader

    Thanks,

    Anuj

    Saturday, August 28, 2010 4:44 AM
  • How do I reference the file from inside the unit test?  that is, the file is in a data directory of the unit test project.  How do I reference that file without using the absolute path (which will not be the same between different engineers on the project)
    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider
    Saturday, August 28, 2010 1:25 PM
  • Hi,

    You can add the file in your project. Then you can open your testrunconfig/testsetting and add the file in your deployment item (refer http://msdn.microsoft.com/en-us/library/ms182475(VS.90).aspx ). You then need to refere the file by its relative path i.e. just its name since it will be in the same working directory as your test dll.

    Thanks,

    Anuj

    Saturday, August 28, 2010 8:23 PM
  • Hi Anuj,

    I know it's been a while, but I never did quite get this working.  I can get it to work if I specify the file I want explicity in the DeploymentItem attribute, but I can't figure out how to get the testproject to copy all files in my TestData subdirectory of my test project.  the "out" directory never seems to get any of those files.  I've also never even been able to get it to copy individudal files in the subdirectory.

    I've tried:

    @"TestData\*.*     {this causes the test not to run}

    @"TestData           {nothing moves}

    "TestData\\"          {nothing moves}

    @"TestData\myfile.txt"   {myfile.txt does not move}

    THanks for help on this.

        [DeploymentItem(@"TestData\"),
        DeploymentItem("CrSyncFilesUtils.dll"), TestMethod()]
        public void SyncFileSystemReplicasTest()
        {
    

    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider
    Friday, April 1, 2011 6:36 PM
  • Hi Peter,

    Suppose your solution file is at "C:\TestProjectA" and your test data folder is at "C:\TestProjectA\TestProjectA\TestData""

    You should then specify this attribute

    [

    DeploymentItem("@TestProjectA\\TestData"

    )]

    Thanks,

    Anuj

    Tuesday, April 5, 2011 4:30 AM