Answered Reading metro unit test data files?

  • Friday, August 17, 2012 3:59 PM
     
     

    I'm new to developing for Windows 8/Metro/Windows Store, and I'm starting by writing unit tests for some existing code. I have successfully created a unit test project (C# for Metro/Windows Store) and a test class with empty methods that run fine. I've also figured out how to setup to do class-level and test-level initialization.

    My class-level initialization method needs to copy some data files (added to the project and marked as "Embedded Resource"/"Copy if newer") to the place where the code I'm testing expects them to be, which is C:\Users\[USER]\Local\Packages\]PACKAGEFAMILYNAME]\LocalState. I don't see any way to do that, however.

    Where I'm stuck is how exactly to get the path to the source and destination files (and then after that, finding the classes to do the I/O). It looks like techniques (e.g., DeploymentItem attribute) and classes/methods that used to be available for Windows apps are no longer available in Metro due to its new security model.

    The class-level initialization method is given a TestContext input, but that doesn't appear to contain anything useful for this purpose.

    Can somebody point me in the right direction?

    Thanks,

    Mark Peters

All Replies

  • Friday, August 17, 2012 8:35 PM
     
     Answered

    I figured it out. In my project I created a ConfigFiles folder, marked each file as "Content" and "Copy if newer" and added my files in that directory. The following method copies the specified file from "ConfigFiles" to C:\Users\[USER]\Local\Packages\]PACKAGEFAMILYNAME]\LocalState\:

            static private async Task copyFile(string fileName)
            {
                var srcUri = new Uri("ms-appx:///ConfigFiles/" + fileName);
                StorageFile srcFile = await StorageFile.GetFileFromApplicationUriAsync(srcUri);
                await srcFile.CopyAsync(ApplicationData.Current.LocalFolder, fileName,
                        NameCollisionOption.ReplaceExisting);
            }

    The method is static because it is being called from

            [ClassInitialize()]
            static public async Task Initialize(TestContext context)
            {
                await copyFile("file1.xml");
                await copyFile("file2.xml");
                await copyFile("file3.xml");

                // Other initialization needed once for the entire class...

            }

    There may be some issue with this that I haven't found yet, but so far it looks good.

    Mark Peters

    • Marked As Answer by Mark Peters Friday, August 17, 2012 8:36 PM
    •  
  • Monday, August 20, 2012 5:22 AM
    Moderator
     
     

    Hi Mark,

    Glad to hear you have had your issue resolved. And thanks for sharing us your solution here, it will benefit to other community members who encountered the similar issue.

    Thanks.


    Vicky Song [MSFT]
    MSDN Community Support | Feedback to us