mutants, tests and zombies - testcon moscow · a mutation is a controlled change in the sut which...

36
Mutants, tests and zombies Alexander Todorov http://atodorov.org @atodorov_ TestCon Moscow 2018

Upload: others

Post on 12-Oct-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

Mutants, tests and zombies

Alexander Todorovhttp://atodorov.org

@atodorov_TestCon Moscow 2018

Page 2: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

S

Page 3: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used
Page 4: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used
Page 5: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

Is my test suite good enough

?

Page 6: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

What is a mutation ?

Page 7: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

- if age > 18 then

+ if age < 18 then

buy(beer)

Page 8: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

Федеральный закон о государственном регулировании

производства и оборота этилового спирта, алкогольной

и спиртосодержащей продукции says 18+

Page 9: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

- had_lunch = True

+ had_lunch = False

Page 10: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used
Page 11: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

while True do

- break

+ continue

Page 12: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used
Page 13: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

Definition

A mutation is a controlled change in the SUT

which produces a valid syntax but changes

program behavior!

Mutation testing can be used to evaluate the

quality of existing test suites!

It can find missing or inadequate tests or dead

code!

Page 14: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

Algorithm for mutation testing

for operator in mutation-operators:

for site in operator.sites(code):

for mutant in operator.mutate(site):

run_tests(mutant)

https://github.com/sixty-north/cosmic-ray

Page 15: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

3 rules to kill mutants !

run_tests(vanilla_code) MUST PASS

if run_tests(mutated_code) == FAIL:

mutant dies

if run_tests(mutated_code) == PASS:

zombie

Page 16: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used
Page 17: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

class Desk < ApplicationRecord

... skip ...

def upcase_language_code

language_code&.upcase

end

end

Page 18: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

@@ -1,4 +1,4 @@

def upcase_language_code

- language_code&.upcase

+ nil

end

Page 19: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

еxpect(

desk.upcase_language_code

).to_not be_nil

Page 20: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

@@ -1,4 +1,4 @@

def upcase_language_code

- language_code&.upcase

+ self

end

Page 21: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

еxpect(

desk.upcase_language_code

).to be_instance_of(String)

Page 22: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

@@ -1,4 +1,4 @@

def upcase_language_code

- language_code&.upcase

+ language_code

end

Page 23: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

desk.language_code = 'ru'

expect(

desk.upcase_language_code

).to eq('RU')

Page 24: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

@@ -1,4 +1,4 @@

def upcase_language_code

- language_code&.upcase

+ language_code.upcase

end

Page 25: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

desk.language_code = nil

expect(

desk.upcase_language_code

).to be_nil

Page 26: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

Is my test suite good enough

?

Page 27: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used
Page 28: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

http://kaner.com/pdfs/negligence_and_testing_coverage.pdf

Page 29: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

Mutation testing vs. coverage

●http://bit.ly/GTAC2015mutation

●http://bit.ly/GTAC2016coverage

●http://bit.ly/MutationVsCoverage

Page 30: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

SUT: pelican-ab, v0.2.1

AB_EXPERIMENT="control" make github

AB_EXPERIMENT="123" make github

AB_EXPERIMENT="xy" make github

●100% branch & mutation coverage

●Bug when

DELETE_OUTPUT_DIRECTORY=True

Page 31: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

SUT: pelican-ab, v0.2.1

●Recursion loop bug when class is inherited

- super(self.__class__, self).....

+ super(PelicanAbExperimentWriter, self)....

●See Pylint #1109 for more info

Page 32: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

100 % of any metric

is useless

Page 33: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

How slow is it ?

https://github.com/rhinstaller/pykickstart/

●Full mutation test run: > 6 days

●After divide and conquer & fail fast: 6.5 hrs

Page 34: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

When to use

mutation testing ?

Page 35: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

Mutation testing tools

●Python – sixty-north/cosmic-ray

●Ruby – mbj/mutant

●Java – hcoles/pitest

●JavaScript - stryker-mutator/stryker

●PHP - padraic/humbug

●LLVM based: C, C++, Rust, etc – mull-

project/mull

https://mutation-testing.slack.com/

Page 36: Mutants, tests and zombies - TestCon Moscow · A mutation is a controlled change in the SUT which produces a valid syntax but changes program behavior! Mutation testing can be used

СПАСИБО