TDD: why might it be wrong to let app code know it is being tested, not run?

后端 未结 5 465
说谎
说谎 2021-01-19 19:35

In this thread, Brian (the only answerer) says \"Your code should be written in such a fashion that it is testing-agnostic\"

The single comment says \"Your code shou

5条回答
  •  梦毁少年i
    2021-01-19 19:51

    a lot of tests have package-private access to the app classes

    I would advise against this, the idea of breaking encapsulation in production code feels like the tail wagging the dog to me. It suggests that the classes are too large and / or lack cohesion. TDD, dependency injection / inversion of control, mocking and writing single responsibility classes should remove the need for relaxing visibility.

    The single comment says "Your code should definitely not branch on a global "am I being tested flag".".

    Production code is production code and has no need to know about your tests. There should be no logic concerning tests in there, it's poor separation. Again, dependency injection / inversion of control would allow you to swap in test specific logic at runtime, that won't be included in the production artifact.

提交回复
热议问题