Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

19
  • 1
    "Unit tests are for testing the code of a single class." Sigh. That is not what unit tests are and following that guideline is either going to lead you into bloated classes or counterproductive, hindering tests. Commented Mar 21, 2018 at 22:32
  • 3
    treating "unit" as a euphemism for "class" is unfortunately a very common way of thinking but in reality all this does is (a) encourage "garbage-in, garbage-out" tests, which can totally undermine the validity of entire test suites and (b) enforce boundaries that should be free to change, which can cripple productivity. I wish more people would listen to their pain and challenge this preconception. Commented Mar 21, 2018 at 22:40
  • 3
    No such confusion. Test units of behaviour, not units of code. There is nothing about the concept of a unit test - except in the widespread cargo cult hive mind of software testing - that implies that a test unit is coupled to the distinctly OOP concept of a class. And a very damaging misconception that is, too. Commented Mar 21, 2018 at 22:48
  • 1
    I'm not sure precisely what you mean - I'm arguing the exact opposite of what you're implying. Stub/mock hard boundaries (if you absolutely have to) and test a behavioural unit. Through the public API. What that API is depends on what you're building but the footprint should be minimal. Adding abstraction, polymorphism or restructuring code should not cause tests to fail - "unit per class" contradicts this and utterly undermines the value of the tests in the first place. Commented Mar 21, 2018 at 22:54
  • 1
    RobbieDee - I'm talking about the precise opposite. Consider that you may be the one harbouring common misconceptions, revisit what you consider a "unit" and maybe dig a little deeper into what Kent Beck was actually really talking about when he talked about TDD. What most people call TDD now is a cargo cult monstrosity. youtube.com/watch?v=EZ05e7EMOLM Commented Mar 22, 2018 at 8:29