Writing tests for Managed Package

Don't worry about coding defensively against your client's configuration; they're allowed to ignore certain types of test failures. This is noted in the documentation:

If a subscriber creates a validation rule, required field, or trigger on an object referenced by your package, your test might fail if it performs DML on this object. If this object is created only for testing purposes and never at runtime, and the creation fails due to these conflicts, you might be safe to ignore the error and continue the test. Otherwise, contact the customer and determine the impact.

The most important thing is that your code does not fail unit tests during upload, including code coverage. This is the only time you need to fix your code.


As per @sfdcfox's answer, your managed package test cases won't run on package installation as it's near impossible to handle all the customization that could exist in any subscriber org that installs the managed package.

However, say there is something that absolutely must be tested as part of the managed package installation. In this case you can use @IsTest(OnInstall=true) to force either a single test method or an entire classes tests to run.