DZone
DevOps Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > DevOps Zone > What Is A Unit Test?

What Is A Unit Test?

Gil Zilberfeld user avatar by
Gil Zilberfeld
·
May. 09, 14 · DevOps Zone · Interview
Like (0)
Save
Tweet
6.02K Views

Join the DZone community and get the full member experience.

Join For Free

what make unit tests different than other tests? they are full of fail.

going to the wikipedia  definition , you’ll get a vague and unhelpful description, and to summarize: it tests a small piece of code.

in  what language? what is small? and why does that matter?

i feel that many times in software, we’d rather concentrate on the mechanics, rather than on the goal. for example, we talk about mocking (how we do it) while we actually want isolation (which is what we need for our tested code).

first, let’s take a look at our goals for a good unit test:

  1. tell you quickly when there’s a real problem
  2. get you as quickly as possible from “found a problem” to “fixed a problem”

let’s take a closer look, using fail.

functionality : a unit test is a sensor, telling us if a former working functionality no longer works. while feedback is the requirement from every kind of test, the key thing is functionality, and in code terms - logic: if-thens, try-catches and workflows inside the code.

accuracy : a unit test should fail for only two reasons: we broke something and should fix the code (a bug), or we broke something and should fix the test (a changed requirement). in both cases, we’re going to do valuable work. when is it not valuable? example: if the test checked internal implementation, and we changed the implementation but not the functionality, this does not count as a real problem. the code still does what it was meant to, functionality didn’t change. but now we need to fix the test, which is waste. we don’t like waste.

instant : a unit test runs quickly. a suite of hundreds and thousands unit tests runs in a few seconds or minutes. the success of applying a fine-grain sensor array relies on quickness in scale. this usually translates in the tested code to be short and isolated.

locator: when there’s a problem, we need to fix it quickly. part of it is testing a small amount of code. then, there’s more we can do in the test to help us solve the problem. yet, we need to think outside the context of writing the test, though. someone else may break it, in a year or more, after we’ve moved companies twice. in other words, we’re leaving a paper trail to locate the specific problem for someone else. to do that we use accurate naming, readable test code, testing small portion of the code in a specific scenario, isolation from any undetermined or non-specific dependency, and lots of other tricks that will help our unfortunate developer achieve a quick fix.

notice that none of these attributes are about the experience of writing a test. it’s about getting the most value out of it after it’s there.

the only value you get while writing a test, is when the code is not there yet. that’s right, in tdd. in that case, you get all of the above, plus insight about the design and safe incremental progress.

all other kinds of tests, which are also valuable, don’t have all these traits: integration tests don’t locate the problematic code. ui tests are brittle. full system tests are slow. non-functional tests just give you feedback, and exploratory testing is not about functional correctness, but rather on business value (at least should be).

if your test passes the fail test, then it is a unit test.

unit test

Published at DZone with permission of Gil Zilberfeld, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Querying Kafka Topics Using Presto
  • The Evolution of Configuration Management: IaC vs. GitOps
  • Maven Tutorial: Nice and Easy [Video]
  • Top Six Kubernetes Best Practices for Fleet Management

Comments

DevOps Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo