views:

21

answers:

2

I am writing a test code (I'm just a beginner) where I need to give a file name located in my local box. I need to check-in this code as well as the file in TFS, so that when other people take latest version, they get both.

//At my local box
string myFilePath= "D:\BACKUP\samplefile.extension";

For TFS check-in, I gave following path but it failed

string myFilePath= "$MyProjectServer\SomeFolder\samplefile.extension";

Now my question is:

  • How can I specify the file path in my Code, so that after check-in when other people in my team take the latest version, the code will point to right file location in TFS?
A: 

Here's how you can proceed:

  1. Add a file to the root of your unit test project and set Copy to Output Directory: Copy always. For example add test.txt. Because the file is part of the project structure it will be added under source control as all the other files.
  2. Use the [DeploymentItem] attribute in your unit test to indicate which file will be used. You also have the possibility to organize the test files into sub-folders.

    [TestMethod]
    [DeploymentItem("test.txt")]
    public void Index()
    {
        // use relative path to read the file
        var actual = File.ReadAllText("test.txt");
        Assert.AreEqual(actual, "some content");
    }
    
Darin Dimitrov
A: 

Your TFS path string myFilePath= "$MyProjectServer\SomeFolder\samplefile.extension" is in the wrong format.

The correct format for an itemspec is:

string myFilePath= "$/MyProjectServer/SomeFolder/samplefile.extension";

Note the addition of the slash after the $ and the change of backslashes to forward slashes throughout.

Robaticus
When i gave this path, it gives me file not found exception. Do i need to check-in the file first to give the path?
chota
I get this error. $/MyProjectServer/SomeFolder/samplefile.extensionThe file name or path is not valid or it cannot be accessed
chota
I used Relative Path instead and now it is Working Fine. Thanks for replying.
chota