Skip to main content

Timeline for What is the point of unit tests?

Current License: CC BY-SA 4.0

8 events
when toggle format what by license comment
Jul 22, 2023 at 14:29 history edited Adrian McCarthy CC BY-SA 4.0
fixed typos; I must've been sleepy when I drafted this answer
Jun 15, 2023 at 21:40 comment added user_1818839 "Unit tests are the contract" ... In the case of changed requirement : first change the test. Run the testsuite : the unmodified code should fail the test. Then you are in a strong position to modify the code to implement the changed requirement.
Jun 15, 2023 at 18:44 comment added Adrian McCarthy @Corrodias: Absolutely. Any object that acts as an interface to a database session, network connection, clock, etc., probably needs a mock or fake of that thing. But there should be very few of those. I've seen too many designs where every unit that processes data is directly dependent on the data source and thus all of those units need dependency injection so the test can pass in a fake/mock. In nearly all cases, those units should just accept data directly from the caller.
Jun 15, 2023 at 16:37 comment added Corrodias It's often easier (in code) and faster (in execution) to mock/fake an object than it is to set up the real thing. For the most obvious example, consider a repository class that interfaces with a database. One could write a whole SQLite setup script and dependency chain just for their tests... or one could just fake the class to return a specific object expected for the conditions of the tests. That aside, I like the extensive list at the start of this one, so it's definitely a +1.
Jun 15, 2023 at 16:14 comment added Adrian McCarthy @Peter: In many cases, units can be designed with minimal dependencies, so that there are few if any other pieces of code that need to be faked. This is why one of my bullet points was "To discourage unnecessary coupling." The anticipated pain of writing unit tests for code that has lots of coupling would hopefully guide the design towards more isolated, standalone units. Also, I believe that, if unit Foo depends on some specialty container and that container is itself thoroughly unit tested, then there's no point in mocking the container when testing Foo.
Jun 15, 2023 at 12:31 comment added Peter - Reinstate Monica "I've almost never had to make a mock of an interface for a unit test." You must have a different concept of unit test than I do. If you test a "unit" (a class, a module, some small-ish part of code) in isolation (because this is what a unit test does: It disregards the rest of the software): Then you have to, by definition, replace -- mock, stub, whatever -- the parts of the software with which the unit is interfacing.
Jun 15, 2023 at 2:27 comment added Charles Duffy This "unit tests are the contract" point is a critical one I'm not sure I've seen made explicitly enough elsewhere.
Jun 14, 2023 at 16:08 history answered Adrian McCarthy CC BY-SA 4.0