Click here to Skip to main content
15,889,281 members
Articles / Programming Languages / C#
Tip/Trick

Unit Tests Challenges

Rate me:
Please Sign up or sign in to vote.
3.00/5 (1 vote)
1 Apr 2015CPOL2 min read 15.1K   6   1
Fixing third party DLL dependent unit tests with Xcopy

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:
    C#
    // [TestMethod]
    // [TestCategory("ThirdPartyDependent")]
    // public void AddTest()
    
  2. In your build definition, Under Process -> Basic -> Automated Tests -> Test Assembly Category filter, enter:
    C#
    "!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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionTestContext Pin
brazeta2-Apr-15 1:58
brazeta2-Apr-15 1:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.