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

后端 未结 7 1225
执笔经年
执笔经年 2021-01-17 22:20

I am trying to use TDD for my coding practice. I would like to ask should I test with a data that should not happen in a function BUT this data may possibly break your progr

7条回答
  •  独厮守ぢ
    2021-01-17 22:51

    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.

提交回复
热议问题