Mutuj swój kod i sprawdź prawdziwe pokrycie swoich testów!

O mnie

@sawiczpawel

http://pawel.sawicz.eu

Agenda

Co to  testy mutacyjne ?

Dla czego powinniśmy się tym przejmować ?

Kiedy powinno się używać testów mutacyjnych ? 

Wielkie pytania

  1. Co to jest pokrycie testów ?
  2. Czy mój unit test jest prawidłowy ?
  3. Czy pokrycie testów jest dobre ?
  4. Jak mierzysz jakość swoich testów ?

Wszystko pochodzi z natury

Co to jest mutacja w biologii ? 

"In biology, a mutation is a permanent change of the nucleotide sequence of the genome of an organism, virus, or extrachromosomal DNA or other genetic elements" - via wikipedia.com

Jak to można przełożyć do programowania ?

In technology, a mutation is a permanent change of an instruction in a function of a program.

 

Historia testow mutacyjnych

1971 - Richard Lipton

1980 -  Timothy Budd

Problem z mocą obliczeniowa

Ankieta : Jaki procent pokrycia testów (test coverage) jest najlepszy ?

Trywialny przyklad

public int CalculateCO2Emmision(int engineSize)
        {
            if (engineSize < 4)
            {
                return 200;
            }
            else
            {
                return 400;
            }
        }
public void CalculateCO2Emmision_EngineSize3_Return200()
        {
            //arrange
            var checker = new SimpleIf();

            //act
            var result = checker.CalculateCO2Emmision(3);

            //assert
            Assert.AreEqual(200, result);
        }

Co to testy mutacyjne ?

  1. competent programmer hypothesis
  2. coupling effect hypothesis

Czy nasz test pasuje do założeń ?

public int CalculateCO2Emmision(int engineSize)
        {
            if (engineSize <= 4)
            {
                return 200;
            }
            else
            {
                return 400;
            }
        }
public void CalculateCO2Emmision_EngineSize3_Return200()
        {
            //arrange
            var checker = new SimpleIf();

            //act
            var result = checker.CalculateCO2Emmision(3);

            //assert
            Assert.AreEqual(200, result);
        }

Prawidłowy test

        [Test]
        [TestCase(3, 200)]
        [TestCase(4, 400)]
        [TestCase(5, 400)]
        public void CalculateCO2Emmision_EngineSize3_Return200_BestOne
(int engineSize, int expectedC02)
        {
            //arrange
            var checker = new SimpleIf();

            //act
            var result = checker.CalculateCO2Emmision(engineSize);

            //assert
            Assert.AreEqual(expectedC02, result);
        }

Co to jest mutant ?

Jak wyglądają mutanty ?

public int CalculateCO2Emmision(int engineSize)
        {
            if (engineSize <= 4)
            {
                return 200;
            }
            else
            {
                return 400;
            }
        }

Mutant

public int CalculateCO2Emmision(int engineSize)
        {
            if (engineSize < 4)
            {
                return 200;
            }
            else
            {
                return 400;
            }
        }

Wygenerujmy sobie trochę mutantów 

Mutant runner + ILSpy demo

Wiec jeszcze raz jak to ma sie do programowania ?

In technology, a mutation is a permanent change of an instruction in a function of a program.

 

... zabić je wszystkie ogniem!

Mutation score

mutation score = number of mutant killed / total number of mutant

Pokrycie testów 

Nowa ankieta : Czy dalej jesteśmy zadowoleni ze swojego pokrycia testami ?

Nowa ankieta : Dalej jetescie zadowoleni ze swojego pokrycia testami ?

Traditional code coverage tools measure  test coverage of the code, not business intent

Operacje mutacyjne

  1. Operatory arytmetyczne
  2. Operatory logiczne
  3. Dodawanie / usuwanie instrukcji
  4. Wywolywanie nulli

Operatory arytmetyczne

Mutation runner + ILSpy

Operatory logiczne

Mutation runner + ILSpy

Visual Mutator

Gdzie to moze byc przydatne ?

  1. Numerical algorithms
  2. Calculations
  3. Core functionality
  4. A lot of Boolean logic

Porady

  1. Experience to cherry pick right piece of code to mutate
  2. Only time gives you knowledge 
  3. A lot of intuition!
  4. What is right for my org is not right for your org

A wiec wrocmy do naszych pytan

  1. What is my test coverage ?
  2. Is my unit test valid ?
  3. Is test coverage good ?
  4. How do you quantify your tests

Dostepne narzedzia

.NET :

  1. visualmutator
  2. NinjaTurtles

Ruby :

  1. mutant

Java :

  1. pietest

Resources

  1. https://soundcloud.com/arkency/podcast-3-mutation-testing
  2. http://blog.arkency.com/2015/06/how-good-are-your-ruby-tests-testing-your-tests-with-mutant/
  3. https://github.com/mbj/mutant
  4. follow @_m_b_j_ (Markus Schirp)

Dzieki, Pytania ?

@sawiczpawel, pawel@sawicz.eu

Mutation Testing - Testy mutacyjne

By Paweł Sawicz

Mutation Testing - Testy mutacyjne

  • 2,384