Locked Can't read an xml test file for a test

  • Saturday, August 18, 2012 2:43 PM
     
      Has Code

    I am attempting to load XML test data in one of my unit tests (The file property is set to Copy Always), but no matter what I do it doesn't seem to be found.

    Here is my sample test:

    [TestMethod]
    [DeploymentItem(@"WebAlerts.xml")]
    public void CanProcessAlert()
            {
         // ARRANGE
         var webxml = string.Empty;
    
         string hithere = null;
    
         if (File.Exists("WebAlerts.xml"))
             hithere = "Hi There";
         else
             hithere = "Sorry to have missed you";
    
         using (var streamReader = new StreamReader(@"WebAlerts.xml"))
         {
             webxml = streamReader.ReadToEnd();
         }
    
         var rqst = new EventPreprocessorRequest();
    
         rqst.alert = new AlertInfo
         {
              CommonId = new Guid("89bb91fd-7b5c-45dc-aee3-381343557b98"),
              PartnerId = "BuddyCo",
              PartnerReferenceId = 0,
              Type = "WebMonitoringAlert",
              Details = webxml
         };
    
                      ServiceProxy<IEventPreprocessorService>.Invoke("EventPreprocessorService", proxy =>
         {
             // ACT
             var result = proxy.Process(rqst);
    
             // ASSERT
             Assert.IsNotNull(result); 
                    Assert.AreEqual(ServiceAcknowledgement.Success, result.Acknowledgement);
         });
      }
    }

    What am I doing wrong?

All Replies

  • Monday, August 20, 2012 6:15 AM
    Moderator
     
     

    Hi BillyM2010,

    Thank you for posting in the MSDN forum.

    Based on your sample test above, I would like to know where the XML file is located. You can try to use the absolute path in the Deployment statement so that the XML file can be found.

    Also you can run your unit test in Debug mode to check where it starts to fail to read an XML file.

    In addition, I would like to know if you use a XML file as a data source. When you run your unit test, did it pass? If it did not, what’s the error?

    If possible, you could provide some detailed information for resolving this issue.

    Here is a help link about how to read XML data from a Stream:

    http://support.microsoft.com/kb/301228

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us

  • Monday, August 20, 2012 6:55 AM
    Moderator
     
     Answered

    Hi BillyM2010,

    I would like to know if your XML file is not in a folder. If so, I advice that you can add a new folder to your test project and right click this folder  to add a XML file into it. Then click "Test" from menu bar, then "Edit Test Run Configurations", then "Local test run". Then you can click "Deployment" on the left, then click the "Add File" button and add your xml file, then Apply and rerun the test to check if the XML file can be read.

    Note that in this code var streamReader = new StreamReader("@WebAlerts.xml"), delete the @ symbol.

    I hope this will be helpful to you.

    Thanks,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked As Answer by BillyM2010 Monday, August 20, 2012 3:26 PM
    •