Should I unit-test with data that should not be passed in a function (invalid input)?

Yes, you should test those invalid inputs. BUT, if your language has accessibility modifiers and ROBOT() is private you shouldn't be testing it; you should only test public functions/methods.


The functional testing technique is called Boundary Value Analysis.

If your range is 0-100, your boundary values are 0 and 100. You should test, at least:

  • below the boundary value
  • the boundary value
  • above the boundary value

In this case:

-1,0,1,
99,100,101

You assume everything below -1 to -infinity behaves the same, everything between 1-99 behaves the same and everything above 101 behaves the same. This is called Equivalence Partitioning. The ranges outside and between the boundary values are called partitions and you assume that they will have equivalent behaviour.

You should always consider using -1 as a test case to make sure nothing funny happens with negative numbers and a text string if the parameter is not strongly typed.


If the expected outcome is that an exception is thrown with invalid input values, then a test that the exceptions get properly thrown would be appropriate.

Edit:

As I noted in my comment below, if these cases will break your application, you should throw an exception. If it really is logically impossible for these cases to occur, then I would say no, you don't need to throw an exception, and you don't need test cases to cover it.

Note that if your system is well componentized, and this function is one component, the fact that it is logically impossible now doesn't mean it will always be logically impossible. It may be used differently down the road.


You said your method will raise an exception if the argument is not valid.

So, yes you should, because you should test that the exception gets raised.