Unit Tests Challenges - CodeProject

:

Introduction

This tip addresses 2 unique challenges I faced while fixing broken unit tests. I will provide the solution which worked for me.

Background

Recently, I needed to fix ignored unit tests which were failing. After my quick initial investigation, to my surprise, they were not unit tests but integration tests. Whatever, they need to get fixed.

Solutions

After that brief, let me explain the technical challenges here:

  1. Failed unit tests make a call to code. This code is internally trying to get an "exe" (third party) in the executing assembly location. As EXE and related files are not available in the debug folder, they were failing.
  2. Even if I can fix them somehow in dev machine, they are not allowed to run on build server because of third party DLLs which are not supposed to go to build server.

I have figured out a solution for the first challenge using xcopy, VS macro and Post Build events under project properties. I followed the simple steps listed below:

  1. Open the project in VS
  2. Add new folder under the unit tests project
  3. Add the thirdparty DLL under newly added folder
  4. Select the newly added files got to properties. Under properties "Copy to Output Directory" -> change to "Copy if newer'. Save the changes.
  5. Go to unit tests project where unit tests are failing
  6. Under Build Events tab -> Post-build event command line section

    Paste the following command:

    xcopy "$(ProjectDir)\thirdpartyfoldername*.*" "$(TargetDir)" /E /I /F /Y

    xcopy copies the files from the mentioned location to destination post build.

    $(ProjectDir) and $(TargetDir): are VS macros that give the executing project directory and Target Directory is the Output path given under Build section.

    thirdpartyfoldername*.*: tells all the files under the mentioned folder.

    /E /I /F /Y: all files and folders, irrespective of destination files and folders availability, copy and override to destination location

  7. Finally, change the Run the post build event to "On successful build" or whatever makes sense to you.

That's it. Your unit tests should run successfully. On build thirdparty DLLs will be xcopied to debug folder where your unittests DLLs are running.

For the second challenge, I followed the steps given below:

  1. Use TestCategory attribute to unit testmethod as follows:
    // [TestMethod]
    // [TestCategory("ThirdPartyDependent")]
    // public void AddTest()
    
  2. In your build definition, Under Process -> Basic -> Automated Tests -> Test Assembly Category filter, enter:
    "!ThirdPartyDependent"
    

That's all! Your build server skips those testmethods with the given filter attributes.

Conclusion

Hope you enjoyed it and it helps you. I welcome constructive suggestions and opinions.

Finally, it's not what I invented. I Googled and my colleagues helped to get to this. Thanks for all who contributed.