SlideShare une entreprise Scribd logo
1  sur  25
Testing 101
An introduction to automated testing
@ NRGene
Noam Barkai, 2015
Outline for this Presentation
Motivations
Basic concepts
Environments
Testing Principles
Frameworks
Examples
Coding’s great
But it just isn’t enough
If we want to deliver we need...
Agile
Methodologies
Motivations
Basic
Code
Automatic
Testing
Automated
Deployment
Continuous
Integration
Clean
Code
Motivations
Manual testing is tedious, expensive, error-prone and limited in scope.
Automated tests offer better coverage of code → higher quality for our clients →
help keep existing clients happy.
Tests give us confidence in changing our code → more rapid feature turnout for
our clients → help bring in new clients.
Proper test coverage helps catch bugs early in the development cycle → making
development cheaper.
Tests make sure a bug fixed once - stays fixed, and is “documented”.
Tests can verify both progression (new features) and regression (breaking existing
features)
Proper testing assists in architecture and design. A piece of code that’s difficult to
test is usually a “code smell” for poor design.
Basic Concepts
Originators
Kent Beck, co-creator of JUnit (with Erich Gamma), coined the term TDD, pioneered work in
programming design patterns, one of the original signers on the “Agile Manifesto”
Erich Gamma, co-creator of JUnit, part of the “Gang of Four” (GOF), published an influential work on
design patterns.
Robert Martin (A.K.A. “Uncle Bob”), an influential writer and coder, originator of “Clean Code”, also
contributed to the “Agile Manifesto”.
Martin Fowler, author of the influential book: “Refactoring”, defined some of the basic transformations
later automized by most IDE’s we use today, also on the “Agile Manifesto”
Basic Concepts
Unit tests: test the behavior of a single “unit” of code: class, file, module. This is
the most basic level of testing.
Integration tests: test the behavior of several components together. They are
longer-running tests and are usually more complex
System tests: test the behavior of the system as a whole - that the server can
start, that it can connect to the resources that it needs, that it’s in harmony with
other systems.
Other forms of tests: manual tests, performance tests, stress tests, health-checks
Basic Concepts
System
Integration
Unit
Growing:
Execution
Time
Complexity
Fragility
Unit tests
Should run very fast (milliseconds)
Are focused on a single “unit” of logic (class, source-file etc.)
Aim at covering all (or almost all) existing code-paths in the unit of code under
test: this includes exceptional cases, odd inputs, rare combinations.
Are sometimes written prior to the code being tested (q.v TDD)
Should be able to run in isolation, from within the IDE, or command-line
Should be able to run in parallel
Should not require any heavy resources in order to run (so they can run quickly,
and in isolation) - hence no IOC frameworks (Spring etc.), no web-servers, no
databases, no access to external resources
Integration tests
Usually require a more complex setup (database, files, large input, complex state)
Hence, are usually slower (up to several seconds)
Usually aim at testing the integration of various components and their ability to
function together.
Due to exponentially growing complexity, do not aim at covering all code paths,
but only critical / minimal code flows.
Tend to break more easily than unit tests and are more difficult to maintain.
Examples for such tests: DAO layer, DI wiring, “end-to-end” tests
System tests
Are the slowest and most complex of tests (tens of seconds and up)
Are resource intensive
Tend to break the most
Hence should be minimal - only what’s critical and can’t be tested otherwise
Examples: server start-up, basic responsiveness, ecosystem health-check
Unlike unit and integration tests, these tests usually have asynchronous code-
flows: start the server(s); wait; run the test code; wait; verify; shutdown the
server(s); wait
Environments
Our test code runs in very different environments:
Development / research - stand-alone machines. Focus is on speed, isolation
from external resources and easy setup. Main problem is dev frustration (“why
your tests so slow!? why I need to install 50 Perl packages on my machine to test
something written in Java???! W$#^%”)
Build machines - focus is on resource allocation and test isolation. Main problem
is overall group frustration (“why you break master build again?! why you crash
Jenkins machine??!”)
Labs, QA - focus is on automated deployment, switching between multiple
versions (“why can’t I deploy version X.Y.Z?! why no proper database
migration?!”)
Development cycle with tests
Developer checks out code.
Verifies current version / branch’s build is “green” in build system (e.g. Jenkins)
Creates a new feature branch.
Writes tests, writes code, runs all unit tests. Repeat.
Reviews her changes locally and commits code to feature branch.
Commit triggers a build, running most unit and integration tests.
Commit gets reviewed by peers after test build is green.
Code gets merged to master/trunk - triggering a new test build upon merge.
Version is tagged and published to artifactory.
Version is deployed to staging - still more tests: performance, stress, manual.
Version is deployed to production - health checks are run, possibly triggering a
“roll-back”.
Test structure
Almost all tests are comprised of three parts:
1.Setup: this includes instantiating objects needed for tests - whether these are
real objects, stubs or mocks; starting and configuring any resources (such as
database entries, web-services); etc.
2.Running the actual code under test
3.Verifying the result is as expected. Verifications include checking the actual
results returned from the code run; Verifying any “side effects” of the code (entries
written to database, messages in logs etc.); When using mocks verification might
include checking certain methods were called or were not called by the code
under test.
Test structure
Unit test example:
test (“Writer passes result to db”) {
// setup
Database db = mock(Database);
Writer writer = new Writer(db);
// run
writer.writeToDb(new Result(“yo!”));
// verify
Mock.verify(db).insert(“yo!”);
}
Test structure
Integration test example:
test (“Writer passes result to db”) {
// setup
Database db = new Database(); //
slow!
Utils.setupDb(db);
// slow!
Writer writer = new Writer(db);
// run
writer.writeToDb(new Result(“yo!”));
// slow!
// verify
Utils.verifyEntry(db, “some-table”, “yo!”); // slow!
// cleanup
Testing principles
Always prefer the least complex test possible: Unit → Integration → System
Testing principles
Tests should be independent of each other - no assumption as to the order with
which tests are executed should be made (and indeed, many times this order is
unpredictable). Execution order can of course be controlled within each test, by
using before-each, before-all, after-each and after-all methods for setup and
cleanup
Tests should clean up after themselves, leaving the system (global memory, file-
system) in exactly the state it was at prior to the test being run. Hence need to be
weary of: global variables being modified; data persisted to database or to disk
(always use temporary tables and files)
Testing principles
We test our code, not others’ code. If our code uses a database server, we don’t
test that the database knows how to update rows - we assume it works. We test
that our code does what it’s expected to do.
Tests should be very descriptive: what’s being tested, what are the assumptions of
the test.
Tests should not overlap each other - duplicate tests means a greater
maintenance overhead with no addition gain.
Well written tests serve as the best form of documentation for the detailed
behavior of the code under test.
Testing principles
Tests should normally focus on visible behavior. We’re usually interested in the
“what”, and only less often in the “how”.
Hence, tests are usually targeted at “interfaces”, not “implementations”.
A broken test should easily point to where the problem is.
Understanding code is difficult enough - we need to try to keep testing code
simple: setup, run, verify.
Tests are not “cost-free”, they have “drag” - they add another layer of code that
needs to be maintained - hence they should be carefully devised and properly
maintained.
Tests are not a “silver bullet” - they can’t replace proper coding, careful code
reviews, thorough monitoring, general carefulness and responsibility.
Testing principles
Some things are inherently difficult to test with today’s technologies:
Asynchronous code
Extremely exceptional conditions (OOM, deadlocks)
Event-driven, non-deterministic code flows
Integrations of complex systems
UI visual layout
Testing principles
Other things are not worth the trouble of writing automated tests for:
Prototypes, proofs-of-concept and other forms of playing-around-in-the-dark.
Ad-hoc executions.
Code that needs to be “fiddled” into place.
Problem #1: sometimes our prototypes become our products…
Problem #2: what might at first seem like a one-timer, might turn out to be a
repeated task.
Solution #1: think hard beforehand.
Solution #2: Be prepared to write tests “ex-post”.
Frameworks
Java - JUnit
Scala - ScalaTest
Python - unittest (PyUnit)
C - Check
Perl - TAP (Test::More)
Matlab - internal framework (as of r2013a)
Examples from our code base
Final Thoughts
In 99.99% of the situations, not writing tests is a very poor decision.
It’s usually made by engineers who either don’t know better or somehow think
battle-proven methodologies don’t apply to them.
“Tight deadlines” are probably the worst excuse ever given to not writing tests:
testing makes development faster, not slower.
Introducing tests to “legacy code” is much harder than writing code with tests in
the first place - code without tests is a significant “technical debt”.
Mastering automated testing takes time - it might seem rather difficult at first.
The right time to start is now!
Questions?

Contenu connexe

Tendances

Test Driven Development (C#)
Test Driven Development (C#)Test Driven Development (C#)
Test Driven Development (C#)Alan Dean
 
Design for testability as a way to good coding (SOLID and IoC)
Design for testability as a way to good coding (SOLID and IoC)Design for testability as a way to good coding (SOLID and IoC)
Design for testability as a way to good coding (SOLID and IoC)Simone Chiaretta
 
Design for Testability
Design for Testability Design for Testability
Design for Testability Pawel Kalbrun
 
DevOps - Boldly Go for Distro
DevOps - Boldly Go for DistroDevOps - Boldly Go for Distro
DevOps - Boldly Go for DistroPaul Boos
 
Common Challenges & Best Practices for TDD on iOS
Common Challenges & Best Practices for TDD on iOSCommon Challenges & Best Practices for TDD on iOS
Common Challenges & Best Practices for TDD on iOSDerek Lee Boire
 
Software Quality via Unit Testing
Software Quality via Unit TestingSoftware Quality via Unit Testing
Software Quality via Unit TestingShaun Abram
 
Testing and TDD - KoJUG
Testing and TDD - KoJUGTesting and TDD - KoJUG
Testing and TDD - KoJUGlburdz
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development CodeOps Technologies LLP
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesDerek Smith
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)nedirtv
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven DevelopmentJohn Blum
 
How and what to unit test
How and what to unit testHow and what to unit test
How and what to unit testEugenio Lentini
 
Unit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveAlvaro Videla
 
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 DevelopmentShawn Jones
 
Design for Testability: A Tutorial for Devs and Testers
Design for Testability: A Tutorial for Devs and TestersDesign for Testability: A Tutorial for Devs and Testers
Design for Testability: A Tutorial for Devs and TestersTechWell
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testingikhwanhayat
 
Test driven-development
Test driven-developmentTest driven-development
Test driven-developmentDavid Paluy
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@Alex Borsuk
 

Tendances (20)

Test Driven Development (C#)
Test Driven Development (C#)Test Driven Development (C#)
Test Driven Development (C#)
 
Design for testability as a way to good coding (SOLID and IoC)
Design for testability as a way to good coding (SOLID and IoC)Design for testability as a way to good coding (SOLID and IoC)
Design for testability as a way to good coding (SOLID and IoC)
 
Design for Testability
Design for Testability Design for Testability
Design for Testability
 
DevOps - Boldly Go for Distro
DevOps - Boldly Go for DistroDevOps - Boldly Go for Distro
DevOps - Boldly Go for Distro
 
Common Challenges & Best Practices for TDD on iOS
Common Challenges & Best Practices for TDD on iOSCommon Challenges & Best Practices for TDD on iOS
Common Challenges & Best Practices for TDD on iOS
 
Software Quality via Unit Testing
Software Quality via Unit TestingSoftware Quality via Unit Testing
Software Quality via Unit Testing
 
Testing and TDD - KoJUG
Testing and TDD - KoJUGTesting and TDD - KoJUG
Testing and TDD - KoJUG
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
How and what to unit test
How and what to unit testHow and what to unit test
How and what to unit test
 
Unit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = Love
 
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
 
Design for Testability: A Tutorial for Devs and Testers
Design for Testability: A Tutorial for Devs and TestersDesign for Testability: A Tutorial for Devs and Testers
Design for Testability: A Tutorial for Devs and Testers
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
 
Test driven-development
Test driven-developmentTest driven-development
Test driven-development
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@
 

En vedette

Using apache spark to fight world hunger - spark meetup
Using apache spark to fight world hunger - spark meetupUsing apache spark to fight world hunger - spark meetup
Using apache spark to fight world hunger - spark meetupNoam Barkai
 
Automated Testing: Saving time and money
Automated Testing: Saving time and moneyAutomated Testing: Saving time and money
Automated Testing: Saving time and moneyBlayn Parkinson
 
WSO2Con Asia 2014 - Effective Test Automation in an Agile Environment
WSO2Con Asia 2014 - Effective Test Automation in an Agile EnvironmentWSO2Con Asia 2014 - Effective Test Automation in an Agile Environment
WSO2Con Asia 2014 - Effective Test Automation in an Agile EnvironmentWSO2
 
TestComplete – A Sophisticated Automated Testing Tool by SmartBear
TestComplete – A Sophisticated Automated Testing Tool by SmartBearTestComplete – A Sophisticated Automated Testing Tool by SmartBear
TestComplete – A Sophisticated Automated Testing Tool by SmartBearSoftware Testing Solution
 
Agile Testing Framework - The Art of Automated Testing
Agile Testing Framework - The Art of Automated TestingAgile Testing Framework - The Art of Automated Testing
Agile Testing Framework - The Art of Automated TestingDimitri Ponomareff
 
Test Automation Strategies For Agile
Test Automation Strategies For AgileTest Automation Strategies For Agile
Test Automation Strategies For AgileNaresh Jain
 

En vedette (6)

Using apache spark to fight world hunger - spark meetup
Using apache spark to fight world hunger - spark meetupUsing apache spark to fight world hunger - spark meetup
Using apache spark to fight world hunger - spark meetup
 
Automated Testing: Saving time and money
Automated Testing: Saving time and moneyAutomated Testing: Saving time and money
Automated Testing: Saving time and money
 
WSO2Con Asia 2014 - Effective Test Automation in an Agile Environment
WSO2Con Asia 2014 - Effective Test Automation in an Agile EnvironmentWSO2Con Asia 2014 - Effective Test Automation in an Agile Environment
WSO2Con Asia 2014 - Effective Test Automation in an Agile Environment
 
TestComplete – A Sophisticated Automated Testing Tool by SmartBear
TestComplete – A Sophisticated Automated Testing Tool by SmartBearTestComplete – A Sophisticated Automated Testing Tool by SmartBear
TestComplete – A Sophisticated Automated Testing Tool by SmartBear
 
Agile Testing Framework - The Art of Automated Testing
Agile Testing Framework - The Art of Automated TestingAgile Testing Framework - The Art of Automated Testing
Agile Testing Framework - The Art of Automated Testing
 
Test Automation Strategies For Agile
Test Automation Strategies For AgileTest Automation Strategies For Agile
Test Automation Strategies For Agile
 

Similaire à Testing 101

An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testingAdam Stephensen
 
Implementing TDD in for .net Core applications
Implementing TDD in for .net Core applicationsImplementing TDD in for .net Core applications
Implementing TDD in for .net Core applicationsAhmad Kazemi
 
SELJE_Database_Unit_Testing_Slides.pdf
SELJE_Database_Unit_Testing_Slides.pdfSELJE_Database_Unit_Testing_Slides.pdf
SELJE_Database_Unit_Testing_Slides.pdfEric Selje
 
#DOAW16 - DevOps@work Roma 2016 - Testing your databases
#DOAW16 - DevOps@work Roma 2016 - Testing your databases#DOAW16 - DevOps@work Roma 2016 - Testing your databases
#DOAW16 - DevOps@work Roma 2016 - Testing your databasesAlessandro Alpi
 
Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++Hong Le Van
 
Into The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsInto The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsOrtus Solutions, Corp
 
Testing Experience - Evolution of Test Automation Frameworks
Testing Experience - Evolution of Test Automation FrameworksTesting Experience - Evolution of Test Automation Frameworks
Testing Experience - Evolution of Test Automation FrameworksŁukasz Morawski
 
Testing & should i do it
Testing & should i do itTesting & should i do it
Testing & should i do itMartin Sykora
 
Successful Software Projects - What you need to consider
Successful Software Projects - What you need to considerSuccessful Software Projects - What you need to consider
Successful Software Projects - What you need to considerLloydMoore
 
Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Mohamed Taman
 
Automated Software Testing Framework Training by Quontra Solutions
Automated Software Testing Framework Training by Quontra SolutionsAutomated Software Testing Framework Training by Quontra Solutions
Automated Software Testing Framework Training by Quontra SolutionsQuontra Solutions
 

Similaire à Testing 101 (20)

TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
 
An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testing
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Implementing TDD in for .net Core applications
Implementing TDD in for .net Core applicationsImplementing TDD in for .net Core applications
Implementing TDD in for .net Core applications
 
TestDrivenDeveloment
TestDrivenDevelomentTestDrivenDeveloment
TestDrivenDeveloment
 
SELJE_Database_Unit_Testing_Slides.pdf
SELJE_Database_Unit_Testing_Slides.pdfSELJE_Database_Unit_Testing_Slides.pdf
SELJE_Database_Unit_Testing_Slides.pdf
 
#DOAW16 - DevOps@work Roma 2016 - Testing your databases
#DOAW16 - DevOps@work Roma 2016 - Testing your databases#DOAW16 - DevOps@work Roma 2016 - Testing your databases
#DOAW16 - DevOps@work Roma 2016 - Testing your databases
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 
Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
 
Why unit testingl
Why unit testinglWhy unit testingl
Why unit testingl
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
 
Into The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsInto The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applications
 
Testing Experience - Evolution of Test Automation Frameworks
Testing Experience - Evolution of Test Automation FrameworksTesting Experience - Evolution of Test Automation Frameworks
Testing Experience - Evolution of Test Automation Frameworks
 
Testing & should i do it
Testing & should i do itTesting & should i do it
Testing & should i do it
 
Successful Software Projects - What you need to consider
Successful Software Projects - What you need to considerSuccessful Software Projects - What you need to consider
Successful Software Projects - What you need to consider
 
Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.
 
Lecture 21
Lecture 21Lecture 21
Lecture 21
 
Automated Software Testing Framework Training by Quontra Solutions
Automated Software Testing Framework Training by Quontra SolutionsAutomated Software Testing Framework Training by Quontra Solutions
Automated Software Testing Framework Training by Quontra Solutions
 

Dernier

%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...masabamasaba
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...chiefasafspells
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2
 

Dernier (20)

%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 

Testing 101

  • 1. Testing 101 An introduction to automated testing @ NRGene Noam Barkai, 2015
  • 2. Outline for this Presentation Motivations Basic concepts Environments Testing Principles Frameworks Examples
  • 3. Coding’s great But it just isn’t enough If we want to deliver we need... Agile Methodologies Motivations Basic Code Automatic Testing Automated Deployment Continuous Integration Clean Code
  • 4. Motivations Manual testing is tedious, expensive, error-prone and limited in scope. Automated tests offer better coverage of code → higher quality for our clients → help keep existing clients happy. Tests give us confidence in changing our code → more rapid feature turnout for our clients → help bring in new clients. Proper test coverage helps catch bugs early in the development cycle → making development cheaper. Tests make sure a bug fixed once - stays fixed, and is “documented”. Tests can verify both progression (new features) and regression (breaking existing features) Proper testing assists in architecture and design. A piece of code that’s difficult to test is usually a “code smell” for poor design.
  • 5. Basic Concepts Originators Kent Beck, co-creator of JUnit (with Erich Gamma), coined the term TDD, pioneered work in programming design patterns, one of the original signers on the “Agile Manifesto” Erich Gamma, co-creator of JUnit, part of the “Gang of Four” (GOF), published an influential work on design patterns. Robert Martin (A.K.A. “Uncle Bob”), an influential writer and coder, originator of “Clean Code”, also contributed to the “Agile Manifesto”. Martin Fowler, author of the influential book: “Refactoring”, defined some of the basic transformations later automized by most IDE’s we use today, also on the “Agile Manifesto”
  • 6. Basic Concepts Unit tests: test the behavior of a single “unit” of code: class, file, module. This is the most basic level of testing. Integration tests: test the behavior of several components together. They are longer-running tests and are usually more complex System tests: test the behavior of the system as a whole - that the server can start, that it can connect to the resources that it needs, that it’s in harmony with other systems. Other forms of tests: manual tests, performance tests, stress tests, health-checks
  • 8. Unit tests Should run very fast (milliseconds) Are focused on a single “unit” of logic (class, source-file etc.) Aim at covering all (or almost all) existing code-paths in the unit of code under test: this includes exceptional cases, odd inputs, rare combinations. Are sometimes written prior to the code being tested (q.v TDD) Should be able to run in isolation, from within the IDE, or command-line Should be able to run in parallel Should not require any heavy resources in order to run (so they can run quickly, and in isolation) - hence no IOC frameworks (Spring etc.), no web-servers, no databases, no access to external resources
  • 9. Integration tests Usually require a more complex setup (database, files, large input, complex state) Hence, are usually slower (up to several seconds) Usually aim at testing the integration of various components and their ability to function together. Due to exponentially growing complexity, do not aim at covering all code paths, but only critical / minimal code flows. Tend to break more easily than unit tests and are more difficult to maintain. Examples for such tests: DAO layer, DI wiring, “end-to-end” tests
  • 10. System tests Are the slowest and most complex of tests (tens of seconds and up) Are resource intensive Tend to break the most Hence should be minimal - only what’s critical and can’t be tested otherwise Examples: server start-up, basic responsiveness, ecosystem health-check Unlike unit and integration tests, these tests usually have asynchronous code- flows: start the server(s); wait; run the test code; wait; verify; shutdown the server(s); wait
  • 11. Environments Our test code runs in very different environments: Development / research - stand-alone machines. Focus is on speed, isolation from external resources and easy setup. Main problem is dev frustration (“why your tests so slow!? why I need to install 50 Perl packages on my machine to test something written in Java???! W$#^%”) Build machines - focus is on resource allocation and test isolation. Main problem is overall group frustration (“why you break master build again?! why you crash Jenkins machine??!”) Labs, QA - focus is on automated deployment, switching between multiple versions (“why can’t I deploy version X.Y.Z?! why no proper database migration?!”)
  • 12. Development cycle with tests Developer checks out code. Verifies current version / branch’s build is “green” in build system (e.g. Jenkins) Creates a new feature branch. Writes tests, writes code, runs all unit tests. Repeat. Reviews her changes locally and commits code to feature branch. Commit triggers a build, running most unit and integration tests. Commit gets reviewed by peers after test build is green. Code gets merged to master/trunk - triggering a new test build upon merge. Version is tagged and published to artifactory. Version is deployed to staging - still more tests: performance, stress, manual. Version is deployed to production - health checks are run, possibly triggering a “roll-back”.
  • 13. Test structure Almost all tests are comprised of three parts: 1.Setup: this includes instantiating objects needed for tests - whether these are real objects, stubs or mocks; starting and configuring any resources (such as database entries, web-services); etc. 2.Running the actual code under test 3.Verifying the result is as expected. Verifications include checking the actual results returned from the code run; Verifying any “side effects” of the code (entries written to database, messages in logs etc.); When using mocks verification might include checking certain methods were called or were not called by the code under test.
  • 14. Test structure Unit test example: test (“Writer passes result to db”) { // setup Database db = mock(Database); Writer writer = new Writer(db); // run writer.writeToDb(new Result(“yo!”)); // verify Mock.verify(db).insert(“yo!”); }
  • 15. Test structure Integration test example: test (“Writer passes result to db”) { // setup Database db = new Database(); // slow! Utils.setupDb(db); // slow! Writer writer = new Writer(db); // run writer.writeToDb(new Result(“yo!”)); // slow! // verify Utils.verifyEntry(db, “some-table”, “yo!”); // slow! // cleanup
  • 16. Testing principles Always prefer the least complex test possible: Unit → Integration → System
  • 17. Testing principles Tests should be independent of each other - no assumption as to the order with which tests are executed should be made (and indeed, many times this order is unpredictable). Execution order can of course be controlled within each test, by using before-each, before-all, after-each and after-all methods for setup and cleanup Tests should clean up after themselves, leaving the system (global memory, file- system) in exactly the state it was at prior to the test being run. Hence need to be weary of: global variables being modified; data persisted to database or to disk (always use temporary tables and files)
  • 18. Testing principles We test our code, not others’ code. If our code uses a database server, we don’t test that the database knows how to update rows - we assume it works. We test that our code does what it’s expected to do. Tests should be very descriptive: what’s being tested, what are the assumptions of the test. Tests should not overlap each other - duplicate tests means a greater maintenance overhead with no addition gain. Well written tests serve as the best form of documentation for the detailed behavior of the code under test.
  • 19. Testing principles Tests should normally focus on visible behavior. We’re usually interested in the “what”, and only less often in the “how”. Hence, tests are usually targeted at “interfaces”, not “implementations”. A broken test should easily point to where the problem is. Understanding code is difficult enough - we need to try to keep testing code simple: setup, run, verify. Tests are not “cost-free”, they have “drag” - they add another layer of code that needs to be maintained - hence they should be carefully devised and properly maintained. Tests are not a “silver bullet” - they can’t replace proper coding, careful code reviews, thorough monitoring, general carefulness and responsibility.
  • 20. Testing principles Some things are inherently difficult to test with today’s technologies: Asynchronous code Extremely exceptional conditions (OOM, deadlocks) Event-driven, non-deterministic code flows Integrations of complex systems UI visual layout
  • 21. Testing principles Other things are not worth the trouble of writing automated tests for: Prototypes, proofs-of-concept and other forms of playing-around-in-the-dark. Ad-hoc executions. Code that needs to be “fiddled” into place. Problem #1: sometimes our prototypes become our products… Problem #2: what might at first seem like a one-timer, might turn out to be a repeated task. Solution #1: think hard beforehand. Solution #2: Be prepared to write tests “ex-post”.
  • 22. Frameworks Java - JUnit Scala - ScalaTest Python - unittest (PyUnit) C - Check Perl - TAP (Test::More) Matlab - internal framework (as of r2013a)
  • 23. Examples from our code base
  • 24. Final Thoughts In 99.99% of the situations, not writing tests is a very poor decision. It’s usually made by engineers who either don’t know better or somehow think battle-proven methodologies don’t apply to them. “Tight deadlines” are probably the worst excuse ever given to not writing tests: testing makes development faster, not slower. Introducing tests to “legacy code” is much harder than writing code with tests in the first place - code without tests is a significant “technical debt”. Mastering automated testing takes time - it might seem rather difficult at first. The right time to start is now!