I'm unit testing a class which has two dependencies, both of which are external packages (I'm using C# so these are NuGet packages, AutoMapper and CompareNETObjects).

(NOTE: Both are just utilities, related to converting an object from one type to another, and to comparing objects. Neither makes any external calls)

Should I mock these external packages or is it acceptable to assume they are robust building blocks that don't need testing, similar to the .NET libraries?

Or, perhaps, should I decide whether to test each package on a case-by-case basis? Say don't test popular open source packages as we could assume they're well tested but do test less popular ones that don't have as many users or contributers.

I know answers to this question are likely to be opinions but I'd like to find out what experienced unit-testers think.

  • 1
    Do you want the external packages to be part of your tests? If the answer is yes, do not mock them.
    – Andy
    Jun 29, 2016 at 7:27
  • If I mocked them I'd need hand-rolled comparers for the tests and that seems to me to not be testing the real life functionality of the methods under test.
    – Simon Elms
    Jun 29, 2016 at 8:25
  • If you mock the externals, all you have to is say that for inputs X and Y on method Foo you will get output Z.
    – HorusKol
    Jun 29, 2016 at 9:36

1 Answer 1


Mock the externals for your unittests, where applicable

It all depends on what the external library does for you. For example, if you have a class that depends on an external library that works with the file system you probably want to mock that and not perform any actual filesystem operations in your unittest. If you have an external library that does mapping from one object onto another, you'll probably be fine and would want to use that external library in your unittests directly.

Remember that unittests are about a cohesive unit of code, not a single class.

Have broad integrationtests that cover your external library

If you have external libraries, make sure that your integrationtests cover some cases for them. You want to test your integration with those external libraries as part of your tests.

Contribute to the package if it lacks tests

If you find that the external library you are using lacks tests, do not write those tests in your application. Instead, see if you can write the code and submit it to the maintainer and get it in the official version so others can benefit from it too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.