SlideShare a Scribd company logo
www.bsc.es Barcelona, 11th April 2016
Write tests, please
An automated tests overview
Joan López de la Franca
Computational Earth Sciences
2
Agenda
Introduction
Why automated tests?
Tips & tricks
Tools & helpers
Test Driven Development
Real world example
Next steps
References
3
You should write tests
First of all
– You should differentiate between QA team tests and software
developers tests
– Automated tests are also called developer tests, so..
– If you write lines of code..
You should write automated tests!
(Please, try to feel identified with it)
4
Not only unitary
There are a lot of different kinds of automated tests
– Unit tests
– Integration tests
– GUI tests
– End-to-end test
– And so on..
5
The origin..
eXtreme Programming (also named XP)
– Software development agile methodology
– Developed by Kent Beck (Agile Manifesto)
– Intended to improve the software quality
– Include some practices like:
• Pair programming
• Code reviews
• Unit testing
• And so on..
6
..and the goal
What most teams have and what most teams should want
(TestPyramid)
Test suite on most teams Our goal
The lazy guy rule
Why spend a lot of time to do something that could be
done with few minutes?
7
How they should be?
Minimum requirements of a test
– Automated (of course): without manual steps
– Complete (more coverage means more reliability)
– Independent (the order of the factors does not alter..)
– Repeatable (let me see, that it works)
– Well-named (test1, test2, test3 names are not allowed!)
8
Their skeleton
Which parts compose a test?
AAA
– Arrangement: needed steps to prepare the test
– Action: what you want to test
– Assertion: check if everything works
Let’s see an example with Python!
9
Simple example
Imagine that we have a Calculator class with a method sum
which is what we want to test
(Remember the AAA)
class TestCalculatorUnit(TestCase):
def test_that_two_plus_two_is_four(self):
# arrangement
my_calculator = Calculator()
# action
sum_result = my_calculator.sum(2, 2)
# assertion
self.assertEquals(4, sum_result)
A
A
A
10
Why automated testing?
Benefits
– Helps us to make better designs (more changeability)
• Improves the productivity
– Allows us to be much bolder modifying the existing software
– Allows a more evolutionary software development
• Deliver early, deliver often!
• Speeds up user feedback
– Improves the software quality
– Are more repeatable than manual tests
• Provides more reliability
– Documents the code (easy way to know what the code does)
11
Why automated testing?
– Implies less effort than manual tests
• More runs
– Earlier error detection
More productivity, less costs
www.bsc.es
Tips & tricks
13
How to fix a bug?
“Our client has found a bug, fix it, please”
– When this happens, there are some typical questions:
• How to fix it?
• How to find it on the code?
• How to ensure that it won't reappear?
“You know, write tests”
• Make your test successfully passed
• Find why your test is not successfully passing
• If you never breaks the test, it never won't reappear
14
How to deal the dependencies?
Making painless the dependencies maintenance
– We are in the 21st century
• Nobody writes entire systems
• Everybody have dependencies
– Updating dependencies is a painful process for most teams
• The updates may break your system
• You should update (security reasons)
– Write integration tests with your dependencies
• To be notified if something is broken by an update
15
Why are you writing on disk to test?
– Do you remember the last ‘Calculator’ test?
– Now imagine that we want to write to our log ‘high result’ when the
result of the sum is higher than 10.
How do you test this?
–No, the answer is not precisely check if there is a ‘high result’ mark
on the log file
– You will find the right answer on the next section
– SPOILER: You should test only your code.
» Not the native ‘open’ or ‘write’ calls.
www.bsc.es
Tools & helpers
17
Fakes, mocks, stubs and spies
Helpers to simulate the reality
– Classes, libraries or modules
– Different types with different proposals:
• Return fake values
• Check return values
• Check method calls
– Times
– Arguments
– Exists one for most programming languages
• Mockito
• Some languages have included it natively (Python)
18
Mocks example: autosubmit
Mocking the reading of the configuration
class TestAutosubmitConfigUnit(TestCase):
def test_get_property_returns_the_right_value(self):
open_mock = mock_open(read_data=“PROPERTY = value”)
with patch.object(builtins, “open”, open_mock):
obtained_value = AutosubmitConfig.getPropertyValue(“PROPERTY”)
self.assertEquals(value, obtained_value)
19
XUnit frameworks
Testing tools
– Family name given to bunch of testing frameworks
– Originally from JUnit (first widely known)
– Provides a complete API
– Exists one for most programming languages
• Some languages have included it natively (Python)
20
Coverage
Code Coverage
– Simply a measure
– Different types
– How much code tested by a suite
– Some benefits (unit testing):
• Dead code locator
• Error locator (unexpected behavior)
• Reliability unit of our tests
– Very used (see on GitHub open source projects)
www.bsc.es
Test Driven Development
22
Test Driven Development
Aka TDD
– Focusing your development on tests
– Originally from XP
– Defines two main practices (very important!):
• Writing first the automated tests (before code)
– Think about: design, API, needs, …
• Refactoring the code
– Improving it: maintainable, reusable, reliable
23
Test Driven Development (aka TDD)
Lifecycle
24
Test Driven Development (aka TDD)
Benefits
– Leads to modular development
• Reusability
– Forces you to think before write code
– Helps to make quality designs (SOLID)
– Leads to develop exactly what is needed
– Helps develop new features without breaking nothing
– Helps document the work for the next day
– Helps reduce the number of bugs introduced during the development
process
25
Test Driven Development (aka TDD)
Example
– Requirements
• Develop a StringCalculator that can sum numbers
• The sum method can take 0, 1 or 2 numbers
• The result can be 0, the taken number or the sum
• Only base-10 representation allowed
“Don’t forget it! First tests”
26
Test Driven Development (aka TDD)
“Let’s write tests”
class TestStringCalculatorUnit(TestCase):
def test_fails_when_takes_more_than_three_numbers(self):
with self.assertRaises(TooMuchArguments):
StringCalculator.sum(‘3’,’5’,’1’)
def test_normal_sum_with_two_numbers(self):
sum_result = StringCalculator.sum(‘1’,’11’)
self.assertEquals(12, sum_result)
def test_sum_with_one_number_returns_it(self):
sum_result = StringCalculator.sum(‘999’)
self.assertEquals(999, sum_result)
def test_sum_without_parameters_returns_zero(self):
sum_result = StringCalculator.sum()
self.assertEquals(0, sum_result)
27
Test Driven Development (aka TDD)
class StringCalculator:
@staticmethod
def sum(*args):
if len(args) > 2:
raise TooMuchArguments
elif len(args) == 2:
return int(args[0]) + int(args[1])
elif len(args) == 1:
return int(args[0])
else:
return 0
“Tests are failing. Let’s write code”
www.bsc.es
Real World Example
29
Real world example: context
Badoo: dating social network
– Complex web application
– More than 200 millions registered users
– A lot of sensible data
30
Real world example: situation
Do you wanna save $1 million?
– New PHP release is available: PHP7
– Performance improvement lets us to save money
• Less infrastructure needed
– Will all work after the upgrade?
31
Real world example: problem
Nobody says that it will be easy
– Apart from the work that implies the upgrade..
– After upgrade you have hard work
• If you publish with a lot of errors then you will lose a lot of money
• You must test if all the functionalities with all the possibilities are still
working as expected
• How much time do you need?
32
Real world example: succeeded
Finally they’ve done: $1 million saved
– More than 60,000 automated tests
– A lot of errors detected
– Easy problem detection
– Quick way to ensure that system works
www.bsc.es
Next steps
34
Application on the Department
I hope so
– Next time you go to write code..
• Write first a test
(It's an effort that everybody should do)
– Ask me if you need help to start
35
Application on the Department
Future sessions
– Make your suggestions
• Specific technology
• Specific problem
– Coding Dojos
36
References
http://www.martinfowler.com/
https://techblog.badoo.com/
http://googletesting.blogspot.com.es/
https://docs.python.org/dev/library/unittest.html
http://xunitpatterns.com/
www.bsc.es
For further information please contact
joan.lopez@bsc.es
Thank you!

More Related Content

What's hot

Winning the battle against Automated testing
Winning the battle against Automated testingWinning the battle against Automated testing
Winning the battle against Automated testing
Elena Laskavaia
 
Agile Programming Systems # TDD intro
Agile Programming Systems # TDD introAgile Programming Systems # TDD intro
Agile Programming Systems # TDD intro
Vitaliy Kulikov
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
Pablo Villar
 
Illustrated Code (ASE 2021)
Illustrated Code (ASE 2021)Illustrated Code (ASE 2021)
A Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven DevelopmentA Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven Development
Shawn Jones
 
Unit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = Love
Alvaro Videla
 
Automated Integration Regression Testing
Automated Integration Regression TestingAutomated Integration Regression Testing
Automated Integration Regression Testing
Michal Oravec
 
Is this how you hate unit testing?
Is this how you hate unit testing?Is this how you hate unit testing?
Is this how you hate unit testing?
Steven Mak
 
Unit testing (workshop)
Unit testing (workshop)Unit testing (workshop)
Unit testing (workshop)
Foyzul Karim
 
DevOps - Boldly Go for Distro
DevOps - Boldly Go for DistroDevOps - Boldly Go for Distro
DevOps - Boldly Go for Distro
Paul Boos
 
Mocking
MockingMocking
Mocking
eleksdev
 
Unit tests = maintenance hell ?
Unit tests = maintenance hell ? Unit tests = maintenance hell ?
Unit tests = maintenance hell ?
Thibaud Desodt
 
Integration and Unit Testing in Java using Test Doubles like mocks and stubs
Integration and Unit Testing in Java using Test Doubles like mocks and stubsIntegration and Unit Testing in Java using Test Doubles like mocks and stubs
Integration and Unit Testing in Java using Test Doubles like mocks and stubs
Rody Middelkoop
 
Integration Group - Robot Framework
Integration Group - Robot Framework Integration Group - Robot Framework
Integration Group - Robot Framework
OpenDaylight
 
Do Bugs Reside in Complex Code?
Do Bugs Reside in Complex Code?Do Bugs Reside in Complex Code?
Do Bugs Reside in Complex Code?
CISPA Helmholtz Center for Information Security
 
Code Review
Code ReviewCode Review
Code Review
Tu Hoang
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Mireia Sangalo
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And Refactoring
Naresh Jain
 
Software Testing - Tool support for testing (CAST) - Mazenet Solution
Software Testing - Tool support for testing (CAST) - Mazenet SolutionSoftware Testing - Tool support for testing (CAST) - Mazenet Solution
Software Testing - Tool support for testing (CAST) - Mazenet Solution
Mazenetsolution
 
EasyTest Test Automation Tool Introduction
EasyTest Test Automation Tool IntroductionEasyTest Test Automation Tool Introduction
EasyTest Test Automation Tool Introduction
Zhu Zhong
 

What's hot (20)

Winning the battle against Automated testing
Winning the battle against Automated testingWinning the battle against Automated testing
Winning the battle against Automated testing
 
Agile Programming Systems # TDD intro
Agile Programming Systems # TDD introAgile Programming Systems # TDD intro
Agile Programming Systems # TDD intro
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
 
Illustrated Code (ASE 2021)
Illustrated Code (ASE 2021)Illustrated Code (ASE 2021)
Illustrated Code (ASE 2021)
 
A Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven DevelopmentA Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven Development
 
Unit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = Love
 
Automated Integration Regression Testing
Automated Integration Regression TestingAutomated Integration Regression Testing
Automated Integration Regression Testing
 
Is this how you hate unit testing?
Is this how you hate unit testing?Is this how you hate unit testing?
Is this how you hate unit testing?
 
Unit testing (workshop)
Unit testing (workshop)Unit testing (workshop)
Unit testing (workshop)
 
DevOps - Boldly Go for Distro
DevOps - Boldly Go for DistroDevOps - Boldly Go for Distro
DevOps - Boldly Go for Distro
 
Mocking
MockingMocking
Mocking
 
Unit tests = maintenance hell ?
Unit tests = maintenance hell ? Unit tests = maintenance hell ?
Unit tests = maintenance hell ?
 
Integration and Unit Testing in Java using Test Doubles like mocks and stubs
Integration and Unit Testing in Java using Test Doubles like mocks and stubsIntegration and Unit Testing in Java using Test Doubles like mocks and stubs
Integration and Unit Testing in Java using Test Doubles like mocks and stubs
 
Integration Group - Robot Framework
Integration Group - Robot Framework Integration Group - Robot Framework
Integration Group - Robot Framework
 
Do Bugs Reside in Complex Code?
Do Bugs Reside in Complex Code?Do Bugs Reside in Complex Code?
Do Bugs Reside in Complex Code?
 
Code Review
Code ReviewCode Review
Code Review
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And Refactoring
 
Software Testing - Tool support for testing (CAST) - Mazenet Solution
Software Testing - Tool support for testing (CAST) - Mazenet SolutionSoftware Testing - Tool support for testing (CAST) - Mazenet Solution
Software Testing - Tool support for testing (CAST) - Mazenet Solution
 
EasyTest Test Automation Tool Introduction
EasyTest Test Automation Tool IntroductionEasyTest Test Automation Tool Introduction
EasyTest Test Automation Tool Introduction
 

Similar to Write tests, please

Testing practicies not only in scala
Testing practicies not only in scalaTesting practicies not only in scala
Testing practicies not only in scala
Paweł Panasewicz
 
Introduction to Automated Testing
Introduction to Automated TestingIntroduction to Automated Testing
Introduction to Automated Testing
Lars Thorup
 
Introduction to-automated-testing
Introduction to-automated-testingIntroduction to-automated-testing
Introduction to-automated-testing
BestBrains
 
Test parallelization using Jenkins
Test parallelization using JenkinsTest parallelization using Jenkins
Test parallelization using Jenkins
Rogue Wave Software
 
Case study
Case studyCase study
Case study
karan saini
 
Raising the Bar
Raising the BarRaising the Bar
Raising the Bar
Alexandru Bolboaca
 
Unit Testing in R with Testthat - HRUG
Unit Testing in R with Testthat - HRUGUnit Testing in R with Testthat - HRUG
Unit Testing in R with Testthat - HRUG
egoodwintx
 
Usable Software Design
Usable Software DesignUsable Software Design
Usable Software Design
Alexandru Bolboaca
 
Dev buchan 30 proven tips
Dev buchan 30 proven tipsDev buchan 30 proven tips
Dev buchan 30 proven tips
Bill Buchan
 
Automated tests
Automated testsAutomated tests
Automated tests
Damian Sromek
 
Waterfallacies V1 1
Waterfallacies V1 1Waterfallacies V1 1
Waterfallacies V1 1
Jorge Boria
 
Large scale agile development practices
Large scale agile development practicesLarge scale agile development practices
Large scale agile development practices
Skills Matter
 
Test Driven Development and Automation
Test Driven Development and AutomationTest Driven Development and Automation
Test Driven Development and Automation
Mahesh Salaria
 
How to Test PowerShell Code Using Pester
How to Test PowerShell Code Using PesterHow to Test PowerShell Code Using Pester
How to Test PowerShell Code Using Pester
Chris Wahl
 
Transferring Software Testing Tools to Practice
Transferring Software Testing Tools to PracticeTransferring Software Testing Tools to Practice
Transferring Software Testing Tools to Practice
Tao Xie
 
Continuous Delivery - Automate & Build Better Software with Travis CI
Continuous Delivery - Automate & Build Better Software with Travis CIContinuous Delivery - Automate & Build Better Software with Travis CI
Continuous Delivery - Automate & Build Better Software with Travis CI
wajrcs
 
Sample Project using design patterns and agile
Sample Project using design patterns and agileSample Project using design patterns and agile
Sample Project using design patterns and agile
Vicente Bolea
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
ZendCon
 
Automated testing
Automated testingAutomated testing
Automated testing
Aiste Stikliute
 
Testing & should i do it
Testing & should i do itTesting & should i do it
Testing & should i do it
Martin Sykora
 

Similar to Write tests, please (20)

Testing practicies not only in scala
Testing practicies not only in scalaTesting practicies not only in scala
Testing practicies not only in scala
 
Introduction to Automated Testing
Introduction to Automated TestingIntroduction to Automated Testing
Introduction to Automated Testing
 
Introduction to-automated-testing
Introduction to-automated-testingIntroduction to-automated-testing
Introduction to-automated-testing
 
Test parallelization using Jenkins
Test parallelization using JenkinsTest parallelization using Jenkins
Test parallelization using Jenkins
 
Case study
Case studyCase study
Case study
 
Raising the Bar
Raising the BarRaising the Bar
Raising the Bar
 
Unit Testing in R with Testthat - HRUG
Unit Testing in R with Testthat - HRUGUnit Testing in R with Testthat - HRUG
Unit Testing in R with Testthat - HRUG
 
Usable Software Design
Usable Software DesignUsable Software Design
Usable Software Design
 
Dev buchan 30 proven tips
Dev buchan 30 proven tipsDev buchan 30 proven tips
Dev buchan 30 proven tips
 
Automated tests
Automated testsAutomated tests
Automated tests
 
Waterfallacies V1 1
Waterfallacies V1 1Waterfallacies V1 1
Waterfallacies V1 1
 
Large scale agile development practices
Large scale agile development practicesLarge scale agile development practices
Large scale agile development practices
 
Test Driven Development and Automation
Test Driven Development and AutomationTest Driven Development and Automation
Test Driven Development and Automation
 
How to Test PowerShell Code Using Pester
How to Test PowerShell Code Using PesterHow to Test PowerShell Code Using Pester
How to Test PowerShell Code Using Pester
 
Transferring Software Testing Tools to Practice
Transferring Software Testing Tools to PracticeTransferring Software Testing Tools to Practice
Transferring Software Testing Tools to Practice
 
Continuous Delivery - Automate & Build Better Software with Travis CI
Continuous Delivery - Automate & Build Better Software with Travis CIContinuous Delivery - Automate & Build Better Software with Travis CI
Continuous Delivery - Automate & Build Better Software with Travis CI
 
Sample Project using design patterns and agile
Sample Project using design patterns and agileSample Project using design patterns and agile
Sample Project using design patterns and agile
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Automated testing
Automated testingAutomated testing
Automated testing
 
Testing & should i do it
Testing & should i do itTesting & should i do it
Testing & should i do it
 

Recently uploaded

Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
Deuglo Infosystem Pvt Ltd
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
Green Software Development
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
Google
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
kalichargn70th171
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
Alina Yurenko
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
Octavian Nadolu
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Crescat
 
What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
pavan998932
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
timtebeek1
 

Recently uploaded (20)

Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
 
What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
 

Write tests, please

  • 1. www.bsc.es Barcelona, 11th April 2016 Write tests, please An automated tests overview Joan López de la Franca Computational Earth Sciences
  • 2. 2 Agenda Introduction Why automated tests? Tips & tricks Tools & helpers Test Driven Development Real world example Next steps References
  • 3. 3 You should write tests First of all – You should differentiate between QA team tests and software developers tests – Automated tests are also called developer tests, so.. – If you write lines of code.. You should write automated tests! (Please, try to feel identified with it)
  • 4. 4 Not only unitary There are a lot of different kinds of automated tests – Unit tests – Integration tests – GUI tests – End-to-end test – And so on..
  • 5. 5 The origin.. eXtreme Programming (also named XP) – Software development agile methodology – Developed by Kent Beck (Agile Manifesto) – Intended to improve the software quality – Include some practices like: • Pair programming • Code reviews • Unit testing • And so on..
  • 6. 6 ..and the goal What most teams have and what most teams should want (TestPyramid) Test suite on most teams Our goal The lazy guy rule Why spend a lot of time to do something that could be done with few minutes?
  • 7. 7 How they should be? Minimum requirements of a test – Automated (of course): without manual steps – Complete (more coverage means more reliability) – Independent (the order of the factors does not alter..) – Repeatable (let me see, that it works) – Well-named (test1, test2, test3 names are not allowed!)
  • 8. 8 Their skeleton Which parts compose a test? AAA – Arrangement: needed steps to prepare the test – Action: what you want to test – Assertion: check if everything works Let’s see an example with Python!
  • 9. 9 Simple example Imagine that we have a Calculator class with a method sum which is what we want to test (Remember the AAA) class TestCalculatorUnit(TestCase): def test_that_two_plus_two_is_four(self): # arrangement my_calculator = Calculator() # action sum_result = my_calculator.sum(2, 2) # assertion self.assertEquals(4, sum_result) A A A
  • 10. 10 Why automated testing? Benefits – Helps us to make better designs (more changeability) • Improves the productivity – Allows us to be much bolder modifying the existing software – Allows a more evolutionary software development • Deliver early, deliver often! • Speeds up user feedback – Improves the software quality – Are more repeatable than manual tests • Provides more reliability – Documents the code (easy way to know what the code does)
  • 11. 11 Why automated testing? – Implies less effort than manual tests • More runs – Earlier error detection More productivity, less costs
  • 13. 13 How to fix a bug? “Our client has found a bug, fix it, please” – When this happens, there are some typical questions: • How to fix it? • How to find it on the code? • How to ensure that it won't reappear? “You know, write tests” • Make your test successfully passed • Find why your test is not successfully passing • If you never breaks the test, it never won't reappear
  • 14. 14 How to deal the dependencies? Making painless the dependencies maintenance – We are in the 21st century • Nobody writes entire systems • Everybody have dependencies – Updating dependencies is a painful process for most teams • The updates may break your system • You should update (security reasons) – Write integration tests with your dependencies • To be notified if something is broken by an update
  • 15. 15 Why are you writing on disk to test? – Do you remember the last ‘Calculator’ test? – Now imagine that we want to write to our log ‘high result’ when the result of the sum is higher than 10. How do you test this? –No, the answer is not precisely check if there is a ‘high result’ mark on the log file – You will find the right answer on the next section – SPOILER: You should test only your code. » Not the native ‘open’ or ‘write’ calls.
  • 17. 17 Fakes, mocks, stubs and spies Helpers to simulate the reality – Classes, libraries or modules – Different types with different proposals: • Return fake values • Check return values • Check method calls – Times – Arguments – Exists one for most programming languages • Mockito • Some languages have included it natively (Python)
  • 18. 18 Mocks example: autosubmit Mocking the reading of the configuration class TestAutosubmitConfigUnit(TestCase): def test_get_property_returns_the_right_value(self): open_mock = mock_open(read_data=“PROPERTY = value”) with patch.object(builtins, “open”, open_mock): obtained_value = AutosubmitConfig.getPropertyValue(“PROPERTY”) self.assertEquals(value, obtained_value)
  • 19. 19 XUnit frameworks Testing tools – Family name given to bunch of testing frameworks – Originally from JUnit (first widely known) – Provides a complete API – Exists one for most programming languages • Some languages have included it natively (Python)
  • 20. 20 Coverage Code Coverage – Simply a measure – Different types – How much code tested by a suite – Some benefits (unit testing): • Dead code locator • Error locator (unexpected behavior) • Reliability unit of our tests – Very used (see on GitHub open source projects)
  • 22. 22 Test Driven Development Aka TDD – Focusing your development on tests – Originally from XP – Defines two main practices (very important!): • Writing first the automated tests (before code) – Think about: design, API, needs, … • Refactoring the code – Improving it: maintainable, reusable, reliable
  • 23. 23 Test Driven Development (aka TDD) Lifecycle
  • 24. 24 Test Driven Development (aka TDD) Benefits – Leads to modular development • Reusability – Forces you to think before write code – Helps to make quality designs (SOLID) – Leads to develop exactly what is needed – Helps develop new features without breaking nothing – Helps document the work for the next day – Helps reduce the number of bugs introduced during the development process
  • 25. 25 Test Driven Development (aka TDD) Example – Requirements • Develop a StringCalculator that can sum numbers • The sum method can take 0, 1 or 2 numbers • The result can be 0, the taken number or the sum • Only base-10 representation allowed “Don’t forget it! First tests”
  • 26. 26 Test Driven Development (aka TDD) “Let’s write tests” class TestStringCalculatorUnit(TestCase): def test_fails_when_takes_more_than_three_numbers(self): with self.assertRaises(TooMuchArguments): StringCalculator.sum(‘3’,’5’,’1’) def test_normal_sum_with_two_numbers(self): sum_result = StringCalculator.sum(‘1’,’11’) self.assertEquals(12, sum_result) def test_sum_with_one_number_returns_it(self): sum_result = StringCalculator.sum(‘999’) self.assertEquals(999, sum_result) def test_sum_without_parameters_returns_zero(self): sum_result = StringCalculator.sum() self.assertEquals(0, sum_result)
  • 27. 27 Test Driven Development (aka TDD) class StringCalculator: @staticmethod def sum(*args): if len(args) > 2: raise TooMuchArguments elif len(args) == 2: return int(args[0]) + int(args[1]) elif len(args) == 1: return int(args[0]) else: return 0 “Tests are failing. Let’s write code”
  • 29. 29 Real world example: context Badoo: dating social network – Complex web application – More than 200 millions registered users – A lot of sensible data
  • 30. 30 Real world example: situation Do you wanna save $1 million? – New PHP release is available: PHP7 – Performance improvement lets us to save money • Less infrastructure needed – Will all work after the upgrade?
  • 31. 31 Real world example: problem Nobody says that it will be easy – Apart from the work that implies the upgrade.. – After upgrade you have hard work • If you publish with a lot of errors then you will lose a lot of money • You must test if all the functionalities with all the possibilities are still working as expected • How much time do you need?
  • 32. 32 Real world example: succeeded Finally they’ve done: $1 million saved – More than 60,000 automated tests – A lot of errors detected – Easy problem detection – Quick way to ensure that system works
  • 34. 34 Application on the Department I hope so – Next time you go to write code.. • Write first a test (It's an effort that everybody should do) – Ask me if you need help to start
  • 35. 35 Application on the Department Future sessions – Make your suggestions • Specific technology • Specific problem – Coding Dojos
  • 37. www.bsc.es For further information please contact joan.lopez@bsc.es Thank you!