4

I was looking for some correct examples of when people did unit testing (specific scenarios of when unit testing was implemented)


The term Unit Testing seems to frequently be used incorrectly. I am following the definition of unit testing given by the following article:

How just about everyone gets unit testing wrong: "in a nutshell, unit testing is when a developer writes a test method that calls "real" code and lets him or her know when the actual results don't match the expected results."

Also: Unit Testing: "UNIT TESTING is a level of software testing where individual units/ components of a software are tested. A unit is the smallest testable part of any software. In procedural programming, a unit may be an individual program, function, procedure, etc. In object-oriented programming, the smallest unit is a method, which may belong to a base/ super class, abstract class or derived/ child class."


Please, I am just looking for a lot of examples of good Unit test scenarios so I can get an understanding of unit testing best practices. I know and understand that it is difficult to state hard rules for unit testing. However, when you get enough Correct examples of what a unit test is, you start to understand when to implement a unit test.

The guidelines in the link above are actually pretty good and solid, but would still like to just find some examples of real case unit testing because in other articles (and in the article above), you find cases of unit testing horrors and other articles, like the below, which state that unit testing for the most part is a waste of time:

Why Most Unit Testing is Waste — Tests Don’t Improve Quality: Developers Do

Any guidance or correct examples (or pointing to sites with good example cases) would be greatly appreciated. I've been looking for about a few days now. Apologies for the long message.

3
  • 2
    Good question and welcome to SQA! I didn't read the original paper referenced by the last link, but the link itself seems to include a lot of sweeping statements, opinion, and misinformation without much actual data or sound reasoning on why unit tests are a waste. It seems like there's been a trend of throwing out unit tests because they can be written poorly, or with claims of functional tests being "more" useful, while missing that good testing requires both.
    – c32hedge
    Jan 7, 2019 at 17:49
  • Yes, I usually try to find enough articles that say the same thing before coming to a conclusion. The last link I posted was an extreme but quite a few articles to continually warn about poor unit testing but just never really give in-depth advice on proper unit testing.
    – Fractal
    Jan 7, 2019 at 18:58
  • 1
    I read about half of the original paper it referenced and unfortunately, the blog itself seemed to reflect the paper fairly accurately. There were a lot of straw man arguments in there.
    – c32hedge
    Jan 7, 2019 at 21:17

1 Answer 1

3

The Google Testing Blog has a lot of short blogs that often include code examples for both good and bad unit tests. One of my favorites is Change detector tests considered harmful, which, incidentally, addresses some of the points made against [poorly written] unit tests in the last link you referenced, and provides some guidance on avoiding those problems.

1
  • Thanks so much for the links. I haven't hit that blog yet. I found one on unit testing that looks good and the change detector tests has some good advice too.
    – Fractal
    Jan 7, 2019 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.