get the file path in web project when running unit test project
-
Actually im getting error when the below code is executed from unit test project... where import is an xml file in the below code.. XmlTextReader importReader= new XmlTextReader(System.Web.Hosting.HostingEnvironment.MapPath("~/Content/Importcontent/import")); the above code is present in one of the methods in different project(class library) which is used in test method in the unit test project where as the above path is present in web project. and the value of the System.Web.Hosting.HostingEnvironment.MapPath("~/Content/Importcontent/import") is null..ie unable to get the complete path of the file i guess bcos the hostingenvironment is not running(web project) The above code is working fine when running from the webproject ie it returns the exact path. whereas returning null value when running from test project. please let me know how to fetch the file path(where file is present in web project) when running from test project.
-
Actually im getting error when the below code is executed from unit test project... where import is an xml file in the below code.. XmlTextReader importReader= new XmlTextReader(System.Web.Hosting.HostingEnvironment.MapPath("~/Content/Importcontent/import")); the above code is present in one of the methods in different project(class library) which is used in test method in the unit test project where as the above path is present in web project. and the value of the System.Web.Hosting.HostingEnvironment.MapPath("~/Content/Importcontent/import") is null..ie unable to get the complete path of the file i guess bcos the hostingenvironment is not running(web project) The above code is working fine when running from the webproject ie it returns the exact path. whereas returning null value when running from test project. please let me know how to fetch the file path(where file is present in web project) when running from test project.
-
Actually im getting error when the below code is executed from unit test project... where import is an xml file in the below code.. XmlTextReader importReader= new XmlTextReader(System.Web.Hosting.HostingEnvironment.MapPath("~/Content/Importcontent/import")); the above code is present in one of the methods in different project(class library) which is used in test method in the unit test project where as the above path is present in web project. and the value of the System.Web.Hosting.HostingEnvironment.MapPath("~/Content/Importcontent/import") is null..ie unable to get the complete path of the file i guess bcos the hostingenvironment is not running(web project) The above code is working fine when running from the webproject ie it returns the exact path. whereas returning null value when running from test project. please let me know how to fetch the file path(where file is present in web project) when running from test project.
Hi, if you have a file to read in Unit test then you need your file in OUT folder. You need to add those files as DeploymentItem list(.testsettings file >> deployment submenu). you can add Deployment item from testsetting file or by adding class decoration. like,
[DeploymentItem("Content/Importcontent/import")]
after doing one of the above procedure, you can access your file. one more important note : make sure your file property "Copy To Output directory" = Copy Always Thanks -Amit