For instance, insteead of
if (MyFancyObject is null)
throw new InvalidOperationException();
I could simply say
Assert.IsNotNull(MyFancyObject);
But I hardly ever see this done - is there a good reason not to?
For instance, insteead of
if (MyFancyObject is null)
throw new InvalidOperationException();
I could simply say
Assert.IsNotNull(MyFancyObject);
But I hardly ever see this done - is there a good reason not to?
is there a good reason not to?
Yes: packaging.
There's nothing wrong with having code throw an AssertionError.
There's nothing wrong with wrapping the throw into some friendlier looking spelling, like Assert::isNotNull
or Precondition::notNull
.
But the few lines of typing you save creating this thing yourself are not worth the headaches of adding test packages to your dependency graph.
See also: LeftPad.
If this is your convention, you lose the ability to catch specific exceptions.
Unit test methods tend to make certain assumptions about the environment that will not necessarily hold true in implementation code. For example, if I wanted to use this strategy in Python, now everything has to inherit from unittest.TestCase
, and standalone functions are simply not an option anymore.
If you want something more concise for handling common error cases, easy enough to write your own utilities...
If your question is only adressing method input-parameter checking:
I prefer Assert.IsNotNull(MyFancyObject,"MyFancyObject null");
style because