Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. get the file path in web project when running unit test project

get the file path in web project when running unit test project

Scheduled Pinned Locked Moved C#
hostingtestingxmlhelptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    siva455
    wrote on last edited by
    #1

    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.

    B A 2 Replies Last reply
    0
    • S siva455

      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.

      B Offline
      B Offline
      BobJanova
      wrote on last edited by
      #2

      You shouldn't be trying to read files out of your live project in the unit tests. That's, well, not a unit test :)

      1 Reply Last reply
      0
      • S siva455

        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.

        A Offline
        A Offline
        AmitGajjar
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups