次の方法で共有


Cleaning Away The TestResults Folder

When I write code, I usually use the test-driven development approach, which means that I execute a suite of unit tests very often. In Visual Studio 2005, all artifacts of a test run gets copied to a folder beneath the TestResults folder, which is usually placed at the same level as the solution files.

Since these test run artifacts include all the binaries, the TestResults folder can quickly grow to be quite large, and since I run tests as part of my personal development process, I have no need to keep this stuff. You can manually delete the folder once in a while, but that's a bit tedious.

Since Visual Studio 2005 projects are MSBuild files, you can extend various parts of the project definition. Personally, I extend the Clean target to get rid of the TestResults folder. Here's how this works:

You will need to edit your project file (.csproj in my case) in a text editor. You can use Notepad, but why not your favorite XML editor, Visual Studio 2005? Obviously, you can't just double-click on the project file, since this will open the project in Visual Studio 2005, but once the project is loaded, you can right-click on the project in Solution Explorer and select Unload Project. The project will the become unavailable, but you can still right-click it and select Edit... This will open the project file in the XML editor, and when you are done, you can save the file and Reload the project.

Insert this after the Import element in the end of the file:

 <PropertyGroup>
    <TestResultsFolderPath>..\TestResults</TestResultsFolderPath>
</PropertyGroup>

<Target Name="AfterClean">
    <RemoveDir Directories="$(TestResultsFolderPath)"
         Condition="Exists('$(TestResultsFolderPath)')" />
</Target>

As far as I can tell, you have to insert these elements after the Import element, or it will not work, since the Import element imports the definition of the Clean sequence, which includes calling the AfterClean target.

This approach assumes that the TestResults folder is placed in the project file's parent folder, which will typically be the case when you run tests as part of a solution. It would be more correct to associate this cleaning action with the solution itself, but unfortunately, solution files don't offer the same MSBuild extensibility points as project files, so this is a bit of a hack. It also leaves the question that if you have many projects in your solution, which project should then have this extension? For me, it makes most sense to extend the test projects, since they are the cause of the appearance of the TestResults folder in the first place.

Now, you can manually get rid of that TestResults folder by right-clicking on the project (or the solution) in Solution Explorer and selecting Clean, but now you can also do it from the command line with MSBuild:

MSBuild /t:Clean MyProject.csproj

Even better, you can Clean all your projects in one go using the CleanAllProjects build file described in Sayed Ibrahim Hashimi's MSDN Magazine article Inside MSBuild: Compile Apps Your Way With Custom Tasks For The Microsoft Build Engine.

The nice feature of this approach is that you get to decide how to clean your projects on a project-by-project basis, so if you have special considerations for some projects, you can take those into account. It sure is a more sophisticated approach than just searching through your hard drive and deleting all folders named TestResults - who knows, you may have some projects where you actually want to keep your test history.

Comments

  • Anonymous
    July 13, 2006
    Thomas Murphy on To Table Design or Not To Table Design.

    Rob Caron on stpBA StoryBoarding for Team...
  • Anonymous
    September 18, 2006
    &amp;nbsp; Link to ploeh blog : Cleaning Away The TestResults Folder
  • Anonymous
    June 25, 2007
    About a year ago, I wrote about an automated way to clean up the TestResults folder created by VSTS.