Running unit tests on nested functions

后端 未结 6 2035
野的像风
野的像风 2020-12-16 17:32

I come from the Java world, where you can hide variables and functions and then run unit tests against them using reflection. I have used nested functions to hide implement

6条回答
  •  -上瘾入骨i
    2020-12-16 18:23

    inner doesn't exist until outer makes it. You should either move inner up to a toplevel function for testability, or have the outer test test all the possible execution paths of itself and inner.

    Do note that the inner function isn't a simple function, it's a closure. Consider this case:

    def outer(a):
        b = compute_something_from(a)
        def inner():
            do_something_with(a, b)
    

    That's the standard testability trade-off. If your cyclomatic complexity is too high, your tests will be too numerous.

提交回复
热议问题