.NET 4.0 code contracts - How will they affect unit testing?

前端 未结 4 699
孤城傲影
孤城傲影 2020-12-30 19:23

For example this article introduces them.

What is the benefit?

Static analysis seems cool but at the same time it would prevent the ability to pass null as

4条回答
  •  爱一瞬间的悲伤
    2020-12-30 19:56

    Well it will not interfere with unit-testing in general. But as I saw you mentioned something about TDD.

    If I think about it from that perspective I guess it could/may change the procedure from the standard one

    • create method (just signature)
    • create Unit test -> implement the test
    • run the test: let it fail
    • implement the method, hack it to the end just to make it working
    • run the test: see it pass
    • refactor your (possibly messy) method body
    • (re-run the test just to see you've not broken anything)

    This would be the really hard-full-featured unit-testing procedure. In such a context I guess you could insert code contracts between the 1st and 2nd point like

    • create method (just signature)
    • insert code contracts for the methods input parameters
    • create Unit test -> implement the test
    • ...

    The advantage I see at the moment is that you can write easier unit tests in the sense that you wouldn't have to check every possible path since some is already taken into account by your defined contracts. It just gives you additional checking, but it wouldn't replace unit testing since there will always be more logic within the code, more path that have to be tested with unit tests as usual.

    Edit

    Another possibility I didn't consider before would be to add the code contracts in the refactoring part. Basically as additional way of assuring things. But that would somehow be redundant and since people don't like to do redundant stuff...

提交回复
热议问题