SlideShare a Scribd company logo
1 of 37
Download to read offline
ART OF UNIT TESTING
how to do it right
About speaker
Few words about me:
◦ 10 years in software development
◦ Technical Lead at V.I.Tech
◦ Like culture
Hello!
I AM DMYTRO PATSERKOVSKYI
What we are talk about
◦ about developers thoughts;
◦ about code quality;
◦ about unit tests tips;
◦ about developers.
Developer, Level 1.
Developer, Level 1: thoughts
◦ code is good;
◦ code always works properly;
◦ if you think that you found a bug,
read above.
Developer, Level 2.
Developer, Level 2: thoughts
◦ as a usually, code works properly;
◦ code could have bugs;
◦ it is to expensive to write unit tests;
◦ we could test our code manually,
time to time;
Developer, Level 5.
? WHAT IS UNIT TEST?
@BeforeClass
public void testBeforeSuite() {
prepareMailService();
}
@Test
public void testEmailService() {
MailService service= generateMailService();
assertNotNull(service);
}
@Test
public void testSending() {
MailService service= generateMailService();
assertTrue(service.sendMail(subject, message));
}
Setup
Test Method
Assertion
Developer, Level 5: thoughts
◦ bugs happens;
◦ we should prevent bugs;
◦ it is cheaper to write unit tests;
◦ one unit test per class / method
should be enough.
Developer, Level 11.
? DO UNIT TESTS HAVE PRINCIPLES?
unit
simple
independent
complete
isolated
deterministic
? PROJECT ARCHITECTURE
Developer faced with component for sending emails.
Services:
◦ MailService - service for sending emails. Based on
Javax Mail.
◦ LogsStorage - service for collecting logs about
mails sending and flushing it to persistent storage.
◦ LogsMailService - wrapper for work with emails
and logs.
? PRINCIPLES: UNIT & SIMPLE
// Bad
@Test
public void testEmailSending() {
MailService service= generateMailService();
assertNotNull(service);
MailBuilder builder= new MailBuilder();
Mail mail = new MailBuilder().
.newMail()
.addSubject("my mail")
.addRecipient(firstRecipient)
.addRecipient(secondRecipient)
.build();
assertNotNull(mail);
assertEquals(EXPECT, mail.getSubject());
...
assertTrue(service.sendMail(mail));
}
// Good
@BeforeTest
public void initialize() {
service= generateMailService();
}
@Test
public void testEmailSending() {
assertTrue(
service.sendMail(prepareMail()));
}
each test should cover one piece of code
? PRINCIPLES: UNIT & SIMPLE
? PRINCIPLES: INDEPENDENT
test should runs in random order and in parallel
// Bad
@Test
public void testEmailLogsSuccessful() {
mlStorage.logGood();
assertEquals(1, mlStorage.getCountGood());
assertEquals(1, mlStorage.getCountAll());
}
@Test
public void testEmailLogsFailed() {
mlStorage.logBad();
assertEquals(1, mlStorage.getCountBad());
assertEquals(2, mlStorage.getCountAll());
}
// Good
@Test
public void testEmailLogsSuccessful() {
mlStorage.logGood();
assertEquals(1, mlStorage.getCountGood());
assertEquals(1, mlStorage.getCountAll());
}
@Test
public void testEmailLogsFailed() {
mlStorage.logBad();
assertEquals(1, mlStorage.getCountBad());
assertEquals(1, mlStorage.getCountAll());
}
@AfterMethod
public void cleanAfter() {
mlStorage.cleanState();
}
? PRINCIPLES: COMPLETE
test should cover both: success and fail cases
// Bad
@BeforeTest
public void initialize() {
service= generateMailService();
}
@Test
public void testEmailSending() {
assertTrue(service
.sendMail(prepareGoodMail)));
}
// Good
@BeforeTest
public void initialize() {
service= generateMailService();
}
@Test
public void testEmailSendingSuccess() {
assertTrue(service
.sendMail(prepareGoodMail()));
}
@Test
public void testEmailSendingFailed() {
assertFalse(service
.sendMail(prepareBadMail()));
}
? PRINCIPLES: ISOLATED
encapsulated logic should be covered with separated
tests
// Bad
@Before
public void before() {
service = new LogMailService();
service.setMailService(
new MailService());
}
@Test
public void testLogEmailService() {
assertTrue(
service.sendMail(mail));
}
// Good
@Mock
MailService mlMock;
@Before
public void before() {
service = new LogMailService();
}
@Test
public void testLogEmailService() {
when(mock.sendMail(mail))
.thenReturn(true);
service.setMailService(mlMock)
assertTrue(service.sendMail(mail));
times(1, mlMock.sendMail(mail));
}
? PRINCIPLES: ISOLATED
? PRINCIPLES: DETERMINISTIC
Unit test behaviour should be reproducible
// Bad
@Before
public void before() {
service = new MailService();
}
@Test
public void testEmailService() {
service.sendMail(mail);
delay(1000);
assertEquals(1,
service.getSentCount());
}
// Good
@Mock
Transport transportMock;
@Before
public void before() {
service = new MailService();
service.setTransport(transportMock);
}
@Test
public void testEmailService() {
service.sendMail(mail);
assertEquals(1,
service.getSentCount());
}
Developer, Level 11 25: thoughts
◦ we have to use isolation frameworks;
◦ our code should be testable;
◦ unit tests improves code quality;
◦ unit tests catch bugs early;
◦ unit tests speed up debugging and
troubleshooting.
Developer, Level 30.
? HOW MANY UNITS I SHOULD WRITE TO SLEEP SAFETY?
one per class?
one per method?
90%?
? CODE COVERAGE
Line Coverage
Branch CoverageOverall Coverage
85% - min overall coverage
Developer, Level 30: thoughts
◦ we should know test coverage of
our code;
◦ we should use tools for calculating
coverage;
◦ high code coverage let us make
refactoring safety.
Developer, Level 70.
? WHAT ELSE UNITS COULD GIVE ME?
units as documentation;
units as design;
? UNITS AS DOCUMENTATION
Units are NOT a reference.
Units could (should) be used as helpful
documentation about:
◦ definition of class/interface contract;
◦ corner cases of functionality;
◦ list of behaviour.
? UNITS AS DESIGN
Writing the test first forces you to think
through your design and what it must
accomplish before you write the code.
TDD / BDD
Developer, Level 70: thoughts
◦ unit tests documents our code;
◦ TDD/BDD helps to look at design of
code from different point of view.
What next?
QUICK TIPS
Know what you're
testing
A test written without a clear objective
in mind is easy to spot
Naming Convention
One of the first thing that you notice about
a failed test is its name
Do Repeat Yourself
Readability is very important in unit testing,
so it is acceptable to have duplicate code
Test Results, not
Implementation
Successful unit testing requires writing tests
that would only fail in case of an actual
error or requirement change
Thanks!
ANY QUESTIONS?

More Related Content

What's hot

ITAKE Unconference - Holding down your technical debt with Sonarqube
ITAKE Unconference - Holding down your technical debt with SonarqubeITAKE Unconference - Holding down your technical debt with Sonarqube
ITAKE Unconference - Holding down your technical debt with SonarqubePatroklos Papapetrou (Pat)
 
Tech Talk #5 : Code Analysis SonarQube - Lương Trọng Nghĩa
Tech Talk #5 : Code Analysis SonarQube - Lương Trọng NghĩaTech Talk #5 : Code Analysis SonarQube - Lương Trọng Nghĩa
Tech Talk #5 : Code Analysis SonarQube - Lương Trọng NghĩaNexus FrontierTech
 
Tracking and improving software quality with SonarQube
Tracking and improving software quality with SonarQubeTracking and improving software quality with SonarQube
Tracking and improving software quality with SonarQubePatroklos Papapetrou (Pat)
 
Static code analysis
Static code analysisStatic code analysis
Static code analysisPrancer Io
 
SonarQube - The leading platform for Continuous Code Quality
SonarQube - The leading platform for Continuous Code QualitySonarQube - The leading platform for Continuous Code Quality
SonarQube - The leading platform for Continuous Code QualityLarry Nung
 
Agile code quality metrics
Agile code quality metricsAgile code quality metrics
Agile code quality metricsGil Nahmias
 
Continuous inspection with Sonar
Continuous inspection with SonarContinuous inspection with Sonar
Continuous inspection with Sonargaudol
 
Java Source Code Analysis using SonarQube
Java Source Code Analysis using SonarQubeJava Source Code Analysis using SonarQube
Java Source Code Analysis using SonarQubeAngelin R
 
Code Review
Code ReviewCode Review
Code ReviewTu Hoang
 
Improving Code Quality In Medical Software Through Code Reviews - Vincit Teat...
Improving Code Quality In Medical Software Through Code Reviews - Vincit Teat...Improving Code Quality In Medical Software Through Code Reviews - Vincit Teat...
Improving Code Quality In Medical Software Through Code Reviews - Vincit Teat...VincitOy
 
Top 5 Code Coverage Tools in DevOps
Top 5 Code Coverage Tools in DevOpsTop 5 Code Coverage Tools in DevOps
Top 5 Code Coverage Tools in DevOpsscmGalaxy Inc
 
SonarQube - Should I Stay or Should I Go ?
SonarQube - Should I Stay or Should I Go ? SonarQube - Should I Stay or Should I Go ?
SonarQube - Should I Stay or Should I Go ? Geeks Anonymes
 
Tracking your Technical Debt with Sonarqube
Tracking your Technical Debt with SonarqubeTracking your Technical Debt with Sonarqube
Tracking your Technical Debt with SonarqubePuppet
 
PramodMishra_Profile
PramodMishra_ProfilePramodMishra_Profile
PramodMishra_ProfilePramod Mishra
 
Presentation slides: "How to get 100% code coverage"
Presentation slides: "How to get 100% code coverage" Presentation slides: "How to get 100% code coverage"
Presentation slides: "How to get 100% code coverage" Rapita Systems Ltd
 

What's hot (20)

Sonar Review
Sonar ReviewSonar Review
Sonar Review
 
ITAKE Unconference - Holding down your technical debt with Sonarqube
ITAKE Unconference - Holding down your technical debt with SonarqubeITAKE Unconference - Holding down your technical debt with Sonarqube
ITAKE Unconference - Holding down your technical debt with Sonarqube
 
Tech Talk #5 : Code Analysis SonarQube - Lương Trọng Nghĩa
Tech Talk #5 : Code Analysis SonarQube - Lương Trọng NghĩaTech Talk #5 : Code Analysis SonarQube - Lương Trọng Nghĩa
Tech Talk #5 : Code Analysis SonarQube - Lương Trọng Nghĩa
 
Tracking and improving software quality with SonarQube
Tracking and improving software quality with SonarQubeTracking and improving software quality with SonarQube
Tracking and improving software quality with SonarQube
 
Sonarqube
SonarqubeSonarqube
Sonarqube
 
Static code analysis
Static code analysisStatic code analysis
Static code analysis
 
SonarQube
SonarQubeSonarQube
SonarQube
 
SonarQube - The leading platform for Continuous Code Quality
SonarQube - The leading platform for Continuous Code QualitySonarQube - The leading platform for Continuous Code Quality
SonarQube - The leading platform for Continuous Code Quality
 
Agile code quality metrics
Agile code quality metricsAgile code quality metrics
Agile code quality metrics
 
Continuous inspection with Sonar
Continuous inspection with SonarContinuous inspection with Sonar
Continuous inspection with Sonar
 
Java Source Code Analysis using SonarQube
Java Source Code Analysis using SonarQubeJava Source Code Analysis using SonarQube
Java Source Code Analysis using SonarQube
 
Code Review
Code ReviewCode Review
Code Review
 
Improving Code Quality In Medical Software Through Code Reviews - Vincit Teat...
Improving Code Quality In Medical Software Through Code Reviews - Vincit Teat...Improving Code Quality In Medical Software Through Code Reviews - Vincit Teat...
Improving Code Quality In Medical Software Through Code Reviews - Vincit Teat...
 
Top 5 Code Coverage Tools in DevOps
Top 5 Code Coverage Tools in DevOpsTop 5 Code Coverage Tools in DevOps
Top 5 Code Coverage Tools in DevOps
 
Sonarqube + Docker
Sonarqube + DockerSonarqube + Docker
Sonarqube + Docker
 
SonarQube - Should I Stay or Should I Go ?
SonarQube - Should I Stay or Should I Go ? SonarQube - Should I Stay or Should I Go ?
SonarQube - Should I Stay or Should I Go ?
 
Tracking your Technical Debt with Sonarqube
Tracking your Technical Debt with SonarqubeTracking your Technical Debt with Sonarqube
Tracking your Technical Debt with Sonarqube
 
PramodMishra_Profile
PramodMishra_ProfilePramodMishra_Profile
PramodMishra_Profile
 
Presentation slides: "How to get 100% code coverage"
Presentation slides: "How to get 100% code coverage" Presentation slides: "How to get 100% code coverage"
Presentation slides: "How to get 100% code coverage"
 
Code Coverage
Code CoverageCode Coverage
Code Coverage
 

Similar to Art of Unit Testing: How to Do It Right and Improve Code Quality

Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Gianluca Padovani
 
Tdd is not about testing (C++ version)
Tdd is not about testing (C++ version)Tdd is not about testing (C++ version)
Tdd is not about testing (C++ version)Gianluca Padovani
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Developmentguestc8093a6
 
Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)Lars Thorup
 
Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestSeb Rose
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In ActionJon Kruger
 
Test driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseTest driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseUTC Fire & Security
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDDDror Helper
 
TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012Pietro Di Bello
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven DevelopmentEffective
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven DevelopmentJohn Blanco
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven DevelopmentEffectiveUI
 
Intro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran MizrahiIntro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran MizrahiRan Mizrahi
 
SELJE_Database_Unit_Testing.pdf
SELJE_Database_Unit_Testing.pdfSELJE_Database_Unit_Testing.pdf
SELJE_Database_Unit_Testing.pdfEric Selje
 
Test driven development
Test driven developmentTest driven development
Test driven developmentnamkha87
 
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019Paulo Clavijo
 
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017Ortus Solutions, Corp
 

Similar to Art of Unit Testing: How to Do It Right and Improve Code Quality (20)

Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)
 
Tdd is not about testing (C++ version)
Tdd is not about testing (C++ version)Tdd is not about testing (C++ version)
Tdd is not about testing (C++ version)
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Tdd is not about testing
Tdd is not about testingTdd is not about testing
Tdd is not about testing
 
Unit testing on mobile apps
Unit testing on mobile appsUnit testing on mobile apps
Unit testing on mobile apps
 
Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 
Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under Test
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
 
Test driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseTest driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + Eclipse
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
Intro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran MizrahiIntro To JavaScript Unit Testing - Ran Mizrahi
Intro To JavaScript Unit Testing - Ran Mizrahi
 
SELJE_Database_Unit_Testing.pdf
SELJE_Database_Unit_Testing.pdfSELJE_Database_Unit_Testing.pdf
SELJE_Database_Unit_Testing.pdf
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019
 
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
 

Recently uploaded

Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 

Recently uploaded (20)

Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 

Art of Unit Testing: How to Do It Right and Improve Code Quality

  • 1. ART OF UNIT TESTING how to do it right
  • 2. About speaker Few words about me: ◦ 10 years in software development ◦ Technical Lead at V.I.Tech ◦ Like culture Hello! I AM DMYTRO PATSERKOVSKYI
  • 3. What we are talk about ◦ about developers thoughts; ◦ about code quality; ◦ about unit tests tips; ◦ about developers.
  • 5. Developer, Level 1: thoughts ◦ code is good; ◦ code always works properly; ◦ if you think that you found a bug, read above.
  • 7. Developer, Level 2: thoughts ◦ as a usually, code works properly; ◦ code could have bugs; ◦ it is to expensive to write unit tests; ◦ we could test our code manually, time to time;
  • 9. ? WHAT IS UNIT TEST? @BeforeClass public void testBeforeSuite() { prepareMailService(); } @Test public void testEmailService() { MailService service= generateMailService(); assertNotNull(service); } @Test public void testSending() { MailService service= generateMailService(); assertTrue(service.sendMail(subject, message)); } Setup Test Method Assertion
  • 10. Developer, Level 5: thoughts ◦ bugs happens; ◦ we should prevent bugs; ◦ it is cheaper to write unit tests; ◦ one unit test per class / method should be enough.
  • 12. ? DO UNIT TESTS HAVE PRINCIPLES? unit simple independent complete isolated deterministic
  • 13. ? PROJECT ARCHITECTURE Developer faced with component for sending emails. Services: ◦ MailService - service for sending emails. Based on Javax Mail. ◦ LogsStorage - service for collecting logs about mails sending and flushing it to persistent storage. ◦ LogsMailService - wrapper for work with emails and logs.
  • 14. ? PRINCIPLES: UNIT & SIMPLE // Bad @Test public void testEmailSending() { MailService service= generateMailService(); assertNotNull(service); MailBuilder builder= new MailBuilder(); Mail mail = new MailBuilder(). .newMail() .addSubject("my mail") .addRecipient(firstRecipient) .addRecipient(secondRecipient) .build(); assertNotNull(mail); assertEquals(EXPECT, mail.getSubject()); ... assertTrue(service.sendMail(mail)); } // Good @BeforeTest public void initialize() { service= generateMailService(); } @Test public void testEmailSending() { assertTrue( service.sendMail(prepareMail())); } each test should cover one piece of code
  • 15. ? PRINCIPLES: UNIT & SIMPLE
  • 16. ? PRINCIPLES: INDEPENDENT test should runs in random order and in parallel // Bad @Test public void testEmailLogsSuccessful() { mlStorage.logGood(); assertEquals(1, mlStorage.getCountGood()); assertEquals(1, mlStorage.getCountAll()); } @Test public void testEmailLogsFailed() { mlStorage.logBad(); assertEquals(1, mlStorage.getCountBad()); assertEquals(2, mlStorage.getCountAll()); } // Good @Test public void testEmailLogsSuccessful() { mlStorage.logGood(); assertEquals(1, mlStorage.getCountGood()); assertEquals(1, mlStorage.getCountAll()); } @Test public void testEmailLogsFailed() { mlStorage.logBad(); assertEquals(1, mlStorage.getCountBad()); assertEquals(1, mlStorage.getCountAll()); } @AfterMethod public void cleanAfter() { mlStorage.cleanState(); }
  • 17. ? PRINCIPLES: COMPLETE test should cover both: success and fail cases // Bad @BeforeTest public void initialize() { service= generateMailService(); } @Test public void testEmailSending() { assertTrue(service .sendMail(prepareGoodMail))); } // Good @BeforeTest public void initialize() { service= generateMailService(); } @Test public void testEmailSendingSuccess() { assertTrue(service .sendMail(prepareGoodMail())); } @Test public void testEmailSendingFailed() { assertFalse(service .sendMail(prepareBadMail())); }
  • 18. ? PRINCIPLES: ISOLATED encapsulated logic should be covered with separated tests // Bad @Before public void before() { service = new LogMailService(); service.setMailService( new MailService()); } @Test public void testLogEmailService() { assertTrue( service.sendMail(mail)); } // Good @Mock MailService mlMock; @Before public void before() { service = new LogMailService(); } @Test public void testLogEmailService() { when(mock.sendMail(mail)) .thenReturn(true); service.setMailService(mlMock) assertTrue(service.sendMail(mail)); times(1, mlMock.sendMail(mail)); }
  • 20. ? PRINCIPLES: DETERMINISTIC Unit test behaviour should be reproducible // Bad @Before public void before() { service = new MailService(); } @Test public void testEmailService() { service.sendMail(mail); delay(1000); assertEquals(1, service.getSentCount()); } // Good @Mock Transport transportMock; @Before public void before() { service = new MailService(); service.setTransport(transportMock); } @Test public void testEmailService() { service.sendMail(mail); assertEquals(1, service.getSentCount()); }
  • 21. Developer, Level 11 25: thoughts ◦ we have to use isolation frameworks; ◦ our code should be testable; ◦ unit tests improves code quality; ◦ unit tests catch bugs early; ◦ unit tests speed up debugging and troubleshooting.
  • 23. ? HOW MANY UNITS I SHOULD WRITE TO SLEEP SAFETY? one per class? one per method? 90%?
  • 24. ? CODE COVERAGE Line Coverage Branch CoverageOverall Coverage 85% - min overall coverage
  • 25. Developer, Level 30: thoughts ◦ we should know test coverage of our code; ◦ we should use tools for calculating coverage; ◦ high code coverage let us make refactoring safety.
  • 27. ? WHAT ELSE UNITS COULD GIVE ME? units as documentation; units as design;
  • 28. ? UNITS AS DOCUMENTATION Units are NOT a reference. Units could (should) be used as helpful documentation about: ◦ definition of class/interface contract; ◦ corner cases of functionality; ◦ list of behaviour.
  • 29. ? UNITS AS DESIGN Writing the test first forces you to think through your design and what it must accomplish before you write the code. TDD / BDD
  • 30. Developer, Level 70: thoughts ◦ unit tests documents our code; ◦ TDD/BDD helps to look at design of code from different point of view.
  • 33. Know what you're testing A test written without a clear objective in mind is easy to spot
  • 34. Naming Convention One of the first thing that you notice about a failed test is its name
  • 35. Do Repeat Yourself Readability is very important in unit testing, so it is acceptable to have duplicate code
  • 36. Test Results, not Implementation Successful unit testing requires writing tests that would only fail in case of an actual error or requirement change