52

Tests Coverage is Dead  -  Long Live Mutation Testing

 6 years ago
source link: https://www.tuicool.com/articles/hit/RnuEveF
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
U73mqu3.png!web

In today’s industry, having a great product might not be enough. Competition lurks around the corner, and a key advantage for market leaders is their ability to move fast without breaking things .

Companies are becoming more and more quality obsessed. As we all know, we are very spoiled as end users. How many of you thought and even said the following sentence out loud: “I hate this SOME_FREE_TOOL! It failed to load once when I needed it.” As a result, many companies put product quality on the pedestal and regard it as their holy grail.

Simply searching the web for how to guarantee quality as part of a Continuous Integration/ Continuous Deployment (CI/CD) cycle, one can easily understand that automated tests are a must and unit-tests are one of its keystones.

But “How many tests?” one might ask the input box in his favorite search engine. Unfortunately, the answer here is vague.

Investigating the world wide web, you see Code Coverage is the key index out there, and depending on your code type (functionality, logic, etc.), you can define a random code coverage percentage you want to enforce. Trying to achieve this, you can see that, in most cases, 100% is almost impossible. While 80% is better for your code, is 100 really better than 80? If you add more tests, does this mean your tests suite is stronger? Or is it just causing an exaggerated overhead on adding functionality?

There are some pitfalls in pursuing a random coverage index. From my personal experience, as a hard-working developer, I sometimes feel I am investing too much time in creating and modifying tests during all development and maintenance phases. On the other hand, I once heard from a sneaky and lazy developer friend that he heard some developers are adding tests to improve coverage, but that these tests are not fully covering the functionality — OMG!!!

Improving coverage is easy — just call the function, or load the object, use different inputs — done! And it doesn’t always happen on purpose. Sometimes we simply didn’t think about all edge cases of the unit being tested, perhaps since we were so focused on killing the red lines of uncovered code.

Stop Killing Red Lines — Kill mutants

One great solution is to stop obsessing about test code coverage and take it with a grain of salt. I urge you to foster Mutation Testing as the key index for a test suite strength.

Mutation Testing includes changing the code — a small part at a time — creating mutants, and running the unit tests suite repeatedly. If the tests still pass — meaning that they were not able to identify the mutant (which might represent a wrong behavior) — we can conclude that they are less strong, and vice-versa.

For example, let’s say I need to write a function which verifies if my nuclear reactor should be cooled or if it will have a nuclear meltdown (not good) — this would be true if the temperature is higher than 1000 °C. The simple implementation of function and tests would look something like this:

function isDangerous(temp) {
  if (temp > 1000) {
    return true; //run away now!
  } else {
    return false; //it’s a very nice…
  }
}
test(“test isDangerous”) {
  expect(isDangerous(500)).toBeFalsy();
  expect(isDangerous(2000)).toBeTruthy();
}

The tests are passing and the test coverage would be 100% — yay! However, as we know, code coverage is not the best indicator for test strength and thoroughness .

If we were to execute some mutation testing tool, it would run these tests with a simple mutation of changing ‘>’ to ‘>=’ (which is a common mutation) and would expect my tests to fail (since a possible change in the functionality was made). This exposed a weakness in my tests — I forgot to check and make sure I am handling the number 1000 correctly. Meaning my tests may be good, they are still not thorough enough. Fixing this is easy, and just requires adding:

expect(isDangerous(1000)).toBeFalsy();

This testing methodology can provide us with a much better answer to the question of “How good/strong are our tests?”

A great tool for this is the stryker-mutator , which is simple to execute in CLI, supports main testing frameworks (karma, jest, jasmine, etc.), and whose output is a solid tool for improving your test strength and software resiliency.

jYbume6.png!web

At AppsFlyer, our SDK runs on 9 out of 10 mobile devices worldwide, and our servers are currently handling around 50 billion events daily. We consider ourselves customer-obsessed, and as such, we cannot fail our customers while moving fast and adding features to a rapid CI/CD environment. Therefore, we need to have transparency for the real quality of our system resiliency and tests quality — and I feel mutation testing provides us with such in many projects.

Not afraid of mutants? — Join us!


Recommend

  • 51
    • www.tuicool.com 6 years ago
    • Cache

    OOP Is Dead, Long Live OOP

    Inspiration This blog post is inspired by Aras Pranckevičius ' recent publication of a talk aimed at junior programmers, designed to get them to come to terms with new...

  • 37

    Writing tests should assure us that the code created by us is working correctly. Often we point out the code coverage factor and if we have 100% we can say that implemented solutions are correct. Are you sure? Maybe there is a tool that can give...

  • 37
    • www.tuicool.com 5 years ago
    • Cache

    .NET is Dead, Long Live .NET

    Microsoft has already said that .NET Core is the future of .NET, which means if you haven’t started, you’ll need to start migrating your existing .NET Framework applications to .NET Core. We’ll go over a few reasons to be...

  • 22
    • www.ruma.io 5 years ago
    • Cache

    Ruma is dead, long live Ruma!

    Ruma is dead, long live Ruma! April 10, 2020 by Jonas Platte Rumais a Mat...

  • 18

    Wednesday, 16 December 2020 17:43 Tape is dead – long live tape! By Stephen Withers ...

  • 9
    • robm.me.uk 4 years ago
    • Cache

    Niche is dead; long live niche

    The internet drove to extinction a world full of niche businesses. But even as power is concentrated in ever-fewer large internet platforms, the future belongs to global brands with narrow appeal – the niche multinationals. ...

  • 25
    • blog.rcard.in 4 years ago
    • Cache

    Scala is dead, long live Scala!

    Recently I came across yet another post about the adoption of Scala in the IT world nowadays. The article is “The Rise and Fall of Scala” on DZone. In that post the author...

  • 3
    • www.bigmarker.com 3 years ago
    • Cache

    A/B Testing is Dead, Long Live A/B Testing!

    A/B Testing is Dead, Long Live A/B Testing! by Third Door Media Regis...

  • 10

    An intro to Mutation Testing - or why coverage sucks February 14, 2019 | 14 Minute Read I think it’s safe to assume that everyone hates to be woken up at the middle of the night due to some crazy production bug. The most...

  • 4

    Testing How I use Mutation Testing to Drive Good Test Case Coverage Automated testing is important for efficient software development, catching bugs w...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK