SlideShare a Scribd company logo
1 of 34
Download to read offline
Bad Test, Good Test

       Seb Rose
   Claysnow Limited

      @sebrose
Take an index card

What makes a unit test good or bad?

- Write 3 ‘good’ things on one side

- Write 3 ‘bad’ things on the other side
What is a unit test?
Understandable
@Test
public void scale() {
    Date now = new Date();

    Calendar calBegin = Calendar.getInstance();
    calBegin.setTime(now);
    calBegin.add(Calendar.HOUR, -4);
    Date begin = calBegin.getTime();

    Period p = new Period(4);
    long delta = p.getBegin().getTime() -
                               begin.getTime();
    Assert.assertTrue(
               p.getEnd().compareTo(now) >= 0);
    logger.trace(delta);
    Assert.assertTrue(delta < 10 && delta > -10);
    Assert.assertEquals(
       new Integer(4), new Integer(p.getScale()));
}
[Test]
	   public void asterisk_should_format_to_em()
	   {
        // Context
        Formatter f = new MarkdownFormatter();
        String expected = "This is <em>em</em> text";

        // Action
        String actual = f.Format("This is *em* text");

        // Outcome
        Assert.AreEqual(expected, actual);
	   }
Maintainable
@Test
	   public void smoker_requires_manual_referral()
	   {
        Customer customer = new Customer(“Joe”, “Smith”,
          “12/12/1980”, “Accountant”, “$300,000”, “Yes”,
          “No”);

        Referral referral = underwriting.process(customer);

        Assert.assertEquals(Referral.Manual, referral);
	   }
@Test
	   public void non_smoker_does_not_require_referral()
	   {
        Customer customer = new Customer(“Joe”, “Smith”,
          “12/12/1980”, “Accountant”, “$300,000”, “No”,
          “No”);

        Referral referral = underwriting.process(customer);

        Assert.assertEquals(Referral.None, referral);
	   }
public interface CustomerBuilder {
  public CustomerBuilder standardSingleMale();
  public CustomerBuilder standardSingleFemale();

    public CustomerBuilder smoker();
    public CustomerBuilder hasOccupation(String occupation);

    public Customer build();
}
@Test
	   public void non_smoker_does_not_require_referral()
	   {
        Customer customer = customerBuilder
          .standardSingleMale()
          .build();

        Referral referral = underwriting.process(customer);

        Assert.assertEquals(Referral.None, referral);
	   }
@Test
	   public void smoker_requires_manual_referral()
	   {
        Customer customer = customerBuilder
          .standardSingleFemale()
          .smoker()
          .build();

        Referral referral = underwriting.process(customer);

        Assert.assertEquals(Referral.Manual, referral);
	   }
Repeatable
@Test
public void testRolling() {
    Yahtzee y = Yahtzee.roll();
    assertEquals(y, y);
    Yahtzee y1 = new Yahtzee(1,2,3,4,5);
    Yahtzee y2 = new Yahtzee(1,2,3,4,5);
    assertTrue(y1.equals(y2));
    assertEquals(y1,y2);
    assertFalse(Yahtzee.roll()==Yahtzee.roll());
}
Necessary
[Test]
#region TestCase...
public void TestMapFromRomanNormalToRomanOnes(
                string roman, int times)
{
  var actual = _engine.ToRomanOnes(roman);
  var expected = _engine.Repeat('I', times);

    Assert.That(actual, Is.EqualTo(expected));
}
Granular
@Test
public void testRolling() {
    Yahtzee y = Yahtzee.roll();
    assertEquals(y, y);
    Yahtzee y1 = new Yahtzee(1,2,3,4,5);
    Yahtzee y2 = new Yahtzee(1,2,3,4,5);
    assertTrue(y1.equals(y2));
    assertEquals(y1,y2);
    assertFalse(Yahtzee.roll()==Yahtzee.roll());
}
Fast
Properties of unit testing

    Understandable
     Maintainable
      Repeatable
      Necessary
       Granular
         Fast
Look back at your index card

For each of your ‘good’ things, which of
the properties does it exhibit?

For each of your ‘bad’ things, which of
the properties does it contravene?
Have I missed any?
None of this is a substitute
 for writing testable code.
class PriceCalculator {
  public Price getPrice() {
    if (DateTime.Compare(DateTime.Now,PRICE_RISE_DATE)<0){
       return LOW_PRICE;
    } else {
       return HIGH_PRICE;
    }
  }
}
[Test]
	   public void prices_should_increase_on_2013_03_31()
	   {
      PriceCalculator calc = new PriceCalculator();

        // How can we set the date?
        // ...
	   }
class PriceCalculator {
  public PriceCalculator( SystemClock clock ) {
    clock_ = clock;
  }

    public Price getPrice() {
      if (DateTime.Compare(clock.Now, PRICE_RISE_DATE)<0){
        return LOW_PRICE;
      } else {
        return HIGH_PRICE;
      }
    }
}
[Test]
	   public void prices_should_increase_on_2013_03_31()
	   {
      SystemClock clock = new SystemClock(2013, 03, 31);
      PriceCalculator calc = new PriceCalculator(clock);

        // Now we control the date?
        // ....
	   }
How can we improve?
Properties of unit testing

    Understandable
     Maintainable
      Repeatable
      Necessary
       Granular
         Fast
Unit tests are your executable
           specification



Treat them with as much respect as
       your production code
The Feather’s definition of legacy
code:




      Code that has no tests
Seb Rose
Twitter: 
@sebrose
Blog: 

 www.claysnow.co.uk
E-mail:

 seb@claysnow.co.uk

More Related Content

What's hot

From typing the test to testing the type
From typing the test to testing the typeFrom typing the test to testing the type
From typing the test to testing the typeWim Godden
 
Mocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitMocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitmfrost503
 
Unit testing with PHPUnit
Unit testing with PHPUnitUnit testing with PHPUnit
Unit testing with PHPUnitferca_sl
 
Oracle PL/SQL - Creative Conditional Compilation
Oracle PL/SQL - Creative Conditional CompilationOracle PL/SQL - Creative Conditional Compilation
Oracle PL/SQL - Creative Conditional CompilationScott Wesley
 
Symfony (Unit, Functional) Testing.
Symfony (Unit, Functional) Testing.Symfony (Unit, Functional) Testing.
Symfony (Unit, Functional) Testing.Basel Issmail
 
SfCon: Test Driven Development
SfCon: Test Driven DevelopmentSfCon: Test Driven Development
SfCon: Test Driven DevelopmentAugusto Pascutti
 
Object-oriented Javascript
Object-oriented JavascriptObject-oriented Javascript
Object-oriented JavascriptDaniel Ku
 
Min-Maxing Software Costs - Laracon EU 2015
Min-Maxing Software Costs - Laracon EU 2015Min-Maxing Software Costs - Laracon EU 2015
Min-Maxing Software Costs - Laracon EU 2015Konstantin Kudryashov
 
javascript function & closure
javascript function & closurejavascript function & closure
javascript function & closureHika Maeng
 
Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4jeresig
 
Code moi une RH! (PHP tour 2017)
Code moi une RH! (PHP tour 2017)Code moi une RH! (PHP tour 2017)
Code moi une RH! (PHP tour 2017)Arnaud Langlade
 
Feature Flags Are Flawed: Let's Make Them Better - DPC
Feature Flags Are Flawed: Let's Make Them Better - DPCFeature Flags Are Flawed: Let's Make Them Better - DPC
Feature Flags Are Flawed: Let's Make Them Better - DPCStephen Young
 
Why is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenariosWhy is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenariosDivante
 
Mastering Oracle ADF Bindings
Mastering Oracle ADF BindingsMastering Oracle ADF Bindings
Mastering Oracle ADF BindingsEuegene Fedorenko
 

What's hot (20)

From typing the test to testing the type
From typing the test to testing the typeFrom typing the test to testing the type
From typing the test to testing the type
 
Mocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitMocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnit
 
Unit testing with PHPUnit
Unit testing with PHPUnitUnit testing with PHPUnit
Unit testing with PHPUnit
 
Oracle PL/SQL - Creative Conditional Compilation
Oracle PL/SQL - Creative Conditional CompilationOracle PL/SQL - Creative Conditional Compilation
Oracle PL/SQL - Creative Conditional Compilation
 
Stub you!
Stub you!Stub you!
Stub you!
 
Deep dive into Oracle ADF
Deep dive into Oracle ADFDeep dive into Oracle ADF
Deep dive into Oracle ADF
 
A Test of Strength
A Test of StrengthA Test of Strength
A Test of Strength
 
Symfony (Unit, Functional) Testing.
Symfony (Unit, Functional) Testing.Symfony (Unit, Functional) Testing.
Symfony (Unit, Functional) Testing.
 
Hidden rocks in Oracle ADF
Hidden rocks in Oracle ADFHidden rocks in Oracle ADF
Hidden rocks in Oracle ADF
 
SfCon: Test Driven Development
SfCon: Test Driven DevelopmentSfCon: Test Driven Development
SfCon: Test Driven Development
 
Con5623 pdf 5623_001
Con5623 pdf 5623_001Con5623 pdf 5623_001
Con5623 pdf 5623_001
 
Object-oriented Javascript
Object-oriented JavascriptObject-oriented Javascript
Object-oriented Javascript
 
Min-Maxing Software Costs
Min-Maxing Software CostsMin-Maxing Software Costs
Min-Maxing Software Costs
 
Min-Maxing Software Costs - Laracon EU 2015
Min-Maxing Software Costs - Laracon EU 2015Min-Maxing Software Costs - Laracon EU 2015
Min-Maxing Software Costs - Laracon EU 2015
 
javascript function & closure
javascript function & closurejavascript function & closure
javascript function & closure
 
Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4
 
Code moi une RH! (PHP tour 2017)
Code moi une RH! (PHP tour 2017)Code moi une RH! (PHP tour 2017)
Code moi une RH! (PHP tour 2017)
 
Feature Flags Are Flawed: Let's Make Them Better - DPC
Feature Flags Are Flawed: Let's Make Them Better - DPCFeature Flags Are Flawed: Let's Make Them Better - DPC
Feature Flags Are Flawed: Let's Make Them Better - DPC
 
Why is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenariosWhy is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenarios
 
Mastering Oracle ADF Bindings
Mastering Oracle ADF BindingsMastering Oracle ADF Bindings
Mastering Oracle ADF Bindings
 

Viewers also liked

Mockist vs Classicists TDD
Mockist vs Classicists TDDMockist vs Classicists TDD
Mockist vs Classicists TDDDavid Völkel
 
Fake It Outside-In TDD @XP2017
Fake It Outside-In TDD @XP2017Fake It Outside-In TDD @XP2017
Fake It Outside-In TDD @XP2017David Völkel
 
Unit vs. Integration Tests
Unit vs. Integration TestsUnit vs. Integration Tests
Unit vs. Integration TestsDavid Völkel
 
Mockist vs. Classicists TDD
Mockist vs. Classicists TDDMockist vs. Classicists TDD
Mockist vs. Classicists TDDDavid Völkel
 
Integration Test Hell
Integration Test HellIntegration Test Hell
Integration Test HellDavid Völkel
 
Wie wird mein Code testbar?
Wie wird mein Code testbar?Wie wird mein Code testbar?
Wie wird mein Code testbar?David Völkel
 
Fake It Outside-In TDD
Fake It Outside-In TDDFake It Outside-In TDD
Fake It Outside-In TDDDavid Völkel
 
Fake It Outside-In TDD Workshop @ Clean Code Days
Fake It Outside-In TDD Workshop @ Clean Code Days Fake It Outside-In TDD Workshop @ Clean Code Days
Fake It Outside-In TDD Workshop @ Clean Code Days David Völkel
 
Infrastructure as Code for Beginners
Infrastructure as Code for BeginnersInfrastructure as Code for Beginners
Infrastructure as Code for BeginnersDavid Völkel
 
Transformation Priority Premise @Softwerkskammer MUC
Transformation Priority Premise @Softwerkskammer MUCTransformation Priority Premise @Softwerkskammer MUC
Transformation Priority Premise @Softwerkskammer MUCDavid Völkel
 

Viewers also liked (10)

Mockist vs Classicists TDD
Mockist vs Classicists TDDMockist vs Classicists TDD
Mockist vs Classicists TDD
 
Fake It Outside-In TDD @XP2017
Fake It Outside-In TDD @XP2017Fake It Outside-In TDD @XP2017
Fake It Outside-In TDD @XP2017
 
Unit vs. Integration Tests
Unit vs. Integration TestsUnit vs. Integration Tests
Unit vs. Integration Tests
 
Mockist vs. Classicists TDD
Mockist vs. Classicists TDDMockist vs. Classicists TDD
Mockist vs. Classicists TDD
 
Integration Test Hell
Integration Test HellIntegration Test Hell
Integration Test Hell
 
Wie wird mein Code testbar?
Wie wird mein Code testbar?Wie wird mein Code testbar?
Wie wird mein Code testbar?
 
Fake It Outside-In TDD
Fake It Outside-In TDDFake It Outside-In TDD
Fake It Outside-In TDD
 
Fake It Outside-In TDD Workshop @ Clean Code Days
Fake It Outside-In TDD Workshop @ Clean Code Days Fake It Outside-In TDD Workshop @ Clean Code Days
Fake It Outside-In TDD Workshop @ Clean Code Days
 
Infrastructure as Code for Beginners
Infrastructure as Code for BeginnersInfrastructure as Code for Beginners
Infrastructure as Code for Beginners
 
Transformation Priority Premise @Softwerkskammer MUC
Transformation Priority Premise @Softwerkskammer MUCTransformation Priority Premise @Softwerkskammer MUC
Transformation Priority Premise @Softwerkskammer MUC
 

Similar to Bad test, good test

How to write clean tests
How to write clean testsHow to write clean tests
How to write clean testsDanylenko Max
 
The secret unit testing tools no one has ever told you about
The secret unit testing tools no one has ever told you aboutThe secret unit testing tools no one has ever told you about
The secret unit testing tools no one has ever told you aboutDror Helper
 
Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testingjeresig
 
An introduction to Google test framework
An introduction to Google test frameworkAn introduction to Google test framework
An introduction to Google test frameworkAbner Chih Yi Huang
 
The secret unit testing tools no one ever told you about
The secret unit testing tools no one ever told you aboutThe secret unit testing tools no one ever told you about
The secret unit testing tools no one ever told you aboutDror Helper
 
Como NÃO testar o seu projeto de Software. DevDay 2014
Como NÃO testar o seu projeto de Software. DevDay 2014Como NÃO testar o seu projeto de Software. DevDay 2014
Como NÃO testar o seu projeto de Software. DevDay 2014alexandre freire
 
Developer Test - Things to Know
Developer Test - Things to KnowDeveloper Test - Things to Know
Developer Test - Things to Knowvilniusjug
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good TestsTomek Kaczanowski
 
fixed 3 issues. I am working on remaining.package chegg;public c.pdf
fixed 3 issues. I am working on remaining.package chegg;public c.pdffixed 3 issues. I am working on remaining.package chegg;public c.pdf
fixed 3 issues. I am working on remaining.package chegg;public c.pdfanjanacottonmills
 
Tech In Asia PDC 2017 - Best practice unit testing in mobile apps
Tech In Asia PDC 2017 - Best practice unit testing in mobile appsTech In Asia PDC 2017 - Best practice unit testing in mobile apps
Tech In Asia PDC 2017 - Best practice unit testing in mobile appsFandy Gotama
 
"Unit Testing for Mobile App" by Fandy Gotama (OLX Indonesia)
"Unit Testing for Mobile App" by Fandy Gotama  (OLX Indonesia)"Unit Testing for Mobile App" by Fandy Gotama  (OLX Indonesia)
"Unit Testing for Mobile App" by Fandy Gotama (OLX Indonesia)Tech in Asia ID
 
The Challenge of Choosing FoodFor this forum, please read http.docx
The Challenge of Choosing FoodFor this forum, please read http.docxThe Challenge of Choosing FoodFor this forum, please read http.docx
The Challenge of Choosing FoodFor this forum, please read http.docxmattinsonjanel
 
Powering code reuse with context and render props
Powering code reuse with context and render propsPowering code reuse with context and render props
Powering code reuse with context and render propsForbes Lindesay
 
Clean Code: Chapter 3 Function
Clean Code: Chapter 3 FunctionClean Code: Chapter 3 Function
Clean Code: Chapter 3 FunctionKent Huang
 

Similar to Bad test, good test (20)

How to write clean tests
How to write clean testsHow to write clean tests
How to write clean tests
 
The secret unit testing tools no one has ever told you about
The secret unit testing tools no one has ever told you aboutThe secret unit testing tools no one has ever told you about
The secret unit testing tools no one has ever told you about
 
Writing Good Tests
Writing Good TestsWriting Good Tests
Writing Good Tests
 
Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testing
 
An introduction to Google test framework
An introduction to Google test frameworkAn introduction to Google test framework
An introduction to Google test framework
 
The secret unit testing tools no one ever told you about
The secret unit testing tools no one ever told you aboutThe secret unit testing tools no one ever told you about
The secret unit testing tools no one ever told you about
 
Test doubles
Test doublesTest doubles
Test doubles
 
Developer Testing Tools Roundup
Developer Testing Tools RoundupDeveloper Testing Tools Roundup
Developer Testing Tools Roundup
 
Clean Test Code
Clean Test CodeClean Test Code
Clean Test Code
 
Como NÃO testar o seu projeto de Software. DevDay 2014
Como NÃO testar o seu projeto de Software. DevDay 2014Como NÃO testar o seu projeto de Software. DevDay 2014
Como NÃO testar o seu projeto de Software. DevDay 2014
 
Developer Test - Things to Know
Developer Test - Things to KnowDeveloper Test - Things to Know
Developer Test - Things to Know
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
 
Best practices unit testing
Best practices unit testing Best practices unit testing
Best practices unit testing
 
fixed 3 issues. I am working on remaining.package chegg;public c.pdf
fixed 3 issues. I am working on remaining.package chegg;public c.pdffixed 3 issues. I am working on remaining.package chegg;public c.pdf
fixed 3 issues. I am working on remaining.package chegg;public c.pdf
 
Google guava
Google guavaGoogle guava
Google guava
 
Tech In Asia PDC 2017 - Best practice unit testing in mobile apps
Tech In Asia PDC 2017 - Best practice unit testing in mobile appsTech In Asia PDC 2017 - Best practice unit testing in mobile apps
Tech In Asia PDC 2017 - Best practice unit testing in mobile apps
 
"Unit Testing for Mobile App" by Fandy Gotama (OLX Indonesia)
"Unit Testing for Mobile App" by Fandy Gotama  (OLX Indonesia)"Unit Testing for Mobile App" by Fandy Gotama  (OLX Indonesia)
"Unit Testing for Mobile App" by Fandy Gotama (OLX Indonesia)
 
The Challenge of Choosing FoodFor this forum, please read http.docx
The Challenge of Choosing FoodFor this forum, please read http.docxThe Challenge of Choosing FoodFor this forum, please read http.docx
The Challenge of Choosing FoodFor this forum, please read http.docx
 
Powering code reuse with context and render props
Powering code reuse with context and render propsPowering code reuse with context and render props
Powering code reuse with context and render props
 
Clean Code: Chapter 3 Function
Clean Code: Chapter 3 FunctionClean Code: Chapter 3 Function
Clean Code: Chapter 3 Function
 

More from Seb Rose

Software contracts - Global Enterprise Agile 2023.pdf
Software contracts - Global Enterprise Agile 2023.pdfSoftware contracts - Global Enterprise Agile 2023.pdf
Software contracts - Global Enterprise Agile 2023.pdfSeb Rose
 
Micro-service delivery - without the pitfalls
Micro-service delivery - without the pitfallsMicro-service delivery - without the pitfalls
Micro-service delivery - without the pitfallsSeb Rose
 
DevSecOps - Agile Get-Together 2022.pdf
DevSecOps - Agile Get-Together 2022.pdfDevSecOps - Agile Get-Together 2022.pdf
DevSecOps - Agile Get-Together 2022.pdfSeb Rose
 
Contract testing - Sealights 2022.pdf
Contract testing - Sealights 2022.pdfContract testing - Sealights 2022.pdf
Contract testing - Sealights 2022.pdfSeb Rose
 
Example mapping - slice any story into testable examples - SoCraTes 2022.pdf
Example mapping - slice any story into testable examples - SoCraTes 2022.pdfExample mapping - slice any story into testable examples - SoCraTes 2022.pdf
Example mapping - slice any story into testable examples - SoCraTes 2022.pdfSeb Rose
 
Software testing - learning to walk again (expoQA22)
Software testing - learning to walk again (expoQA22)Software testing - learning to walk again (expoQA22)
Software testing - learning to walk again (expoQA22)Seb Rose
 
DevSecOps - Unicom Agile and DevOps Expo (Adaptive Challenges) 2021
DevSecOps - Unicom Agile and DevOps Expo (Adaptive Challenges) 2021DevSecOps - Unicom Agile and DevOps Expo (Adaptive Challenges) 2021
DevSecOps - Unicom Agile and DevOps Expo (Adaptive Challenges) 2021Seb Rose
 
A brief history of requirements - Unicom 2022
A brief history of requirements  - Unicom 2022A brief history of requirements  - Unicom 2022
A brief history of requirements - Unicom 2022Seb Rose
 
Example mapping (with builds) - ProductWorld 2022
Example mapping (with builds)  - ProductWorld 2022Example mapping (with builds)  - ProductWorld 2022
Example mapping (with builds) - ProductWorld 2022Seb Rose
 
Example mapping - ProductWorld 2022
Example mapping - ProductWorld 2022Example mapping - ProductWorld 2022
Example mapping - ProductWorld 2022Seb Rose
 
No code, low code, machine code QA ATL 2021
No code, low code, machine code   QA ATL 2021No code, low code, machine code   QA ATL 2021
No code, low code, machine code QA ATL 2021Seb Rose
 
No code, low code, machine code QA ATL 2021
No code, low code, machine code   QA ATL 2021No code, low code, machine code   QA ATL 2021
No code, low code, machine code QA ATL 2021Seb Rose
 
No code, low code, machine code - Unicom 2021
No code, low code, machine code -  Unicom 2021No code, low code, machine code -  Unicom 2021
No code, low code, machine code - Unicom 2021Seb Rose
 
BDD: from soup to nuts - The Future of Work Scotland 2021
BDD: from soup to nuts  - The Future of Work Scotland 2021BDD: from soup to nuts  - The Future of Work Scotland 2021
BDD: from soup to nuts - The Future of Work Scotland 2021Seb Rose
 
Contrasting test automation and BDD - 2020
Contrasting test automation and BDD - 2020Contrasting test automation and BDD - 2020
Contrasting test automation and BDD - 2020Seb Rose
 
Are BDD and test automation the same thing? Automation Guild 2021
Are BDD and test automation the same thing?   Automation Guild 2021Are BDD and test automation the same thing?   Automation Guild 2021
Are BDD and test automation the same thing? Automation Guild 2021Seb Rose
 
"Our BDDs are broken!" Lean Agile Exchange 2020
"Our BDDs are broken!"   Lean Agile Exchange 2020"Our BDDs are broken!"   Lean Agile Exchange 2020
"Our BDDs are broken!" Lean Agile Exchange 2020Seb Rose
 
User stories: from good intentions to bad advice - Agile Scotland 2019
User stories: from good intentions to bad advice - Agile Scotland 2019User stories: from good intentions to bad advice - Agile Scotland 2019
User stories: from good intentions to bad advice - Agile Scotland 2019Seb Rose
 
User stories: from good intentions to bad advice - Lean Agile Scotland 2019
User stories: from good intentions to bad advice - Lean Agile Scotland 2019User stories: from good intentions to bad advice - Lean Agile Scotland 2019
User stories: from good intentions to bad advice - Lean Agile Scotland 2019Seb Rose
 
Software contracts or: how I learned to stop worrying and love releasing. Agi...
Software contracts or: how I learned to stop worrying and love releasing. Agi...Software contracts or: how I learned to stop worrying and love releasing. Agi...
Software contracts or: how I learned to stop worrying and love releasing. Agi...Seb Rose
 

More from Seb Rose (20)

Software contracts - Global Enterprise Agile 2023.pdf
Software contracts - Global Enterprise Agile 2023.pdfSoftware contracts - Global Enterprise Agile 2023.pdf
Software contracts - Global Enterprise Agile 2023.pdf
 
Micro-service delivery - without the pitfalls
Micro-service delivery - without the pitfallsMicro-service delivery - without the pitfalls
Micro-service delivery - without the pitfalls
 
DevSecOps - Agile Get-Together 2022.pdf
DevSecOps - Agile Get-Together 2022.pdfDevSecOps - Agile Get-Together 2022.pdf
DevSecOps - Agile Get-Together 2022.pdf
 
Contract testing - Sealights 2022.pdf
Contract testing - Sealights 2022.pdfContract testing - Sealights 2022.pdf
Contract testing - Sealights 2022.pdf
 
Example mapping - slice any story into testable examples - SoCraTes 2022.pdf
Example mapping - slice any story into testable examples - SoCraTes 2022.pdfExample mapping - slice any story into testable examples - SoCraTes 2022.pdf
Example mapping - slice any story into testable examples - SoCraTes 2022.pdf
 
Software testing - learning to walk again (expoQA22)
Software testing - learning to walk again (expoQA22)Software testing - learning to walk again (expoQA22)
Software testing - learning to walk again (expoQA22)
 
DevSecOps - Unicom Agile and DevOps Expo (Adaptive Challenges) 2021
DevSecOps - Unicom Agile and DevOps Expo (Adaptive Challenges) 2021DevSecOps - Unicom Agile and DevOps Expo (Adaptive Challenges) 2021
DevSecOps - Unicom Agile and DevOps Expo (Adaptive Challenges) 2021
 
A brief history of requirements - Unicom 2022
A brief history of requirements  - Unicom 2022A brief history of requirements  - Unicom 2022
A brief history of requirements - Unicom 2022
 
Example mapping (with builds) - ProductWorld 2022
Example mapping (with builds)  - ProductWorld 2022Example mapping (with builds)  - ProductWorld 2022
Example mapping (with builds) - ProductWorld 2022
 
Example mapping - ProductWorld 2022
Example mapping - ProductWorld 2022Example mapping - ProductWorld 2022
Example mapping - ProductWorld 2022
 
No code, low code, machine code QA ATL 2021
No code, low code, machine code   QA ATL 2021No code, low code, machine code   QA ATL 2021
No code, low code, machine code QA ATL 2021
 
No code, low code, machine code QA ATL 2021
No code, low code, machine code   QA ATL 2021No code, low code, machine code   QA ATL 2021
No code, low code, machine code QA ATL 2021
 
No code, low code, machine code - Unicom 2021
No code, low code, machine code -  Unicom 2021No code, low code, machine code -  Unicom 2021
No code, low code, machine code - Unicom 2021
 
BDD: from soup to nuts - The Future of Work Scotland 2021
BDD: from soup to nuts  - The Future of Work Scotland 2021BDD: from soup to nuts  - The Future of Work Scotland 2021
BDD: from soup to nuts - The Future of Work Scotland 2021
 
Contrasting test automation and BDD - 2020
Contrasting test automation and BDD - 2020Contrasting test automation and BDD - 2020
Contrasting test automation and BDD - 2020
 
Are BDD and test automation the same thing? Automation Guild 2021
Are BDD and test automation the same thing?   Automation Guild 2021Are BDD and test automation the same thing?   Automation Guild 2021
Are BDD and test automation the same thing? Automation Guild 2021
 
"Our BDDs are broken!" Lean Agile Exchange 2020
"Our BDDs are broken!"   Lean Agile Exchange 2020"Our BDDs are broken!"   Lean Agile Exchange 2020
"Our BDDs are broken!" Lean Agile Exchange 2020
 
User stories: from good intentions to bad advice - Agile Scotland 2019
User stories: from good intentions to bad advice - Agile Scotland 2019User stories: from good intentions to bad advice - Agile Scotland 2019
User stories: from good intentions to bad advice - Agile Scotland 2019
 
User stories: from good intentions to bad advice - Lean Agile Scotland 2019
User stories: from good intentions to bad advice - Lean Agile Scotland 2019User stories: from good intentions to bad advice - Lean Agile Scotland 2019
User stories: from good intentions to bad advice - Lean Agile Scotland 2019
 
Software contracts or: how I learned to stop worrying and love releasing. Agi...
Software contracts or: how I learned to stop worrying and love releasing. Agi...Software contracts or: how I learned to stop worrying and love releasing. Agi...
Software contracts or: how I learned to stop worrying and love releasing. Agi...
 

Recently uploaded

Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsAndrey Dotsenko
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 

Bad test, good test

  • 1. Bad Test, Good Test Seb Rose Claysnow Limited @sebrose
  • 2. Take an index card What makes a unit test good or bad? - Write 3 ‘good’ things on one side - Write 3 ‘bad’ things on the other side
  • 3. What is a unit test?
  • 5. @Test public void scale() { Date now = new Date(); Calendar calBegin = Calendar.getInstance(); calBegin.setTime(now); calBegin.add(Calendar.HOUR, -4); Date begin = calBegin.getTime(); Period p = new Period(4); long delta = p.getBegin().getTime() - begin.getTime(); Assert.assertTrue( p.getEnd().compareTo(now) >= 0); logger.trace(delta); Assert.assertTrue(delta < 10 && delta > -10); Assert.assertEquals( new Integer(4), new Integer(p.getScale())); }
  • 6. [Test] public void asterisk_should_format_to_em() { // Context Formatter f = new MarkdownFormatter(); String expected = "This is <em>em</em> text"; // Action String actual = f.Format("This is *em* text"); // Outcome Assert.AreEqual(expected, actual); }
  • 8. @Test public void smoker_requires_manual_referral() { Customer customer = new Customer(“Joe”, “Smith”, “12/12/1980”, “Accountant”, “$300,000”, “Yes”, “No”); Referral referral = underwriting.process(customer); Assert.assertEquals(Referral.Manual, referral); }
  • 9. @Test public void non_smoker_does_not_require_referral() { Customer customer = new Customer(“Joe”, “Smith”, “12/12/1980”, “Accountant”, “$300,000”, “No”, “No”); Referral referral = underwriting.process(customer); Assert.assertEquals(Referral.None, referral); }
  • 10. public interface CustomerBuilder { public CustomerBuilder standardSingleMale(); public CustomerBuilder standardSingleFemale(); public CustomerBuilder smoker(); public CustomerBuilder hasOccupation(String occupation); public Customer build(); }
  • 11. @Test public void non_smoker_does_not_require_referral() { Customer customer = customerBuilder .standardSingleMale() .build(); Referral referral = underwriting.process(customer); Assert.assertEquals(Referral.None, referral); }
  • 12. @Test public void smoker_requires_manual_referral() { Customer customer = customerBuilder .standardSingleFemale() .smoker() .build(); Referral referral = underwriting.process(customer); Assert.assertEquals(Referral.Manual, referral); }
  • 14. @Test public void testRolling() { Yahtzee y = Yahtzee.roll(); assertEquals(y, y); Yahtzee y1 = new Yahtzee(1,2,3,4,5); Yahtzee y2 = new Yahtzee(1,2,3,4,5); assertTrue(y1.equals(y2)); assertEquals(y1,y2); assertFalse(Yahtzee.roll()==Yahtzee.roll()); }
  • 16. [Test] #region TestCase... public void TestMapFromRomanNormalToRomanOnes( string roman, int times) { var actual = _engine.ToRomanOnes(roman); var expected = _engine.Repeat('I', times); Assert.That(actual, Is.EqualTo(expected)); }
  • 18. @Test public void testRolling() { Yahtzee y = Yahtzee.roll(); assertEquals(y, y); Yahtzee y1 = new Yahtzee(1,2,3,4,5); Yahtzee y2 = new Yahtzee(1,2,3,4,5); assertTrue(y1.equals(y2)); assertEquals(y1,y2); assertFalse(Yahtzee.roll()==Yahtzee.roll()); }
  • 19. Fast
  • 20. Properties of unit testing Understandable Maintainable Repeatable Necessary Granular Fast
  • 21. Look back at your index card For each of your ‘good’ things, which of the properties does it exhibit? For each of your ‘bad’ things, which of the properties does it contravene?
  • 23. None of this is a substitute for writing testable code.
  • 24. class PriceCalculator { public Price getPrice() { if (DateTime.Compare(DateTime.Now,PRICE_RISE_DATE)<0){ return LOW_PRICE; } else { return HIGH_PRICE; } } }
  • 25. [Test] public void prices_should_increase_on_2013_03_31() { PriceCalculator calc = new PriceCalculator(); // How can we set the date? // ... }
  • 26. class PriceCalculator { public PriceCalculator( SystemClock clock ) { clock_ = clock; } public Price getPrice() { if (DateTime.Compare(clock.Now, PRICE_RISE_DATE)<0){ return LOW_PRICE; } else { return HIGH_PRICE; } } }
  • 27. [Test] public void prices_should_increase_on_2013_03_31() { SystemClock clock = new SystemClock(2013, 03, 31); PriceCalculator calc = new PriceCalculator(clock); // Now we control the date? // .... }
  • 28.
  • 29. How can we improve?
  • 30.
  • 31. Properties of unit testing Understandable Maintainable Repeatable Necessary Granular Fast
  • 32. Unit tests are your executable specification Treat them with as much respect as your production code
  • 33. The Feather’s definition of legacy code: Code that has no tests
  • 34. Seb Rose Twitter: @sebrose Blog: www.claysnow.co.uk E-mail: seb@claysnow.co.uk

Editor's Notes

  1. Untestable code. Test first/TDD
  2. Naming Locality of reference Symbolic constants Distinguish Context/Action/Outcome Documentation - writing for other developers NOT the compiler
  3. Symbolic constant
  4. Naming Locality Context/Action/Outcome
  5. Abstractions White box (TDD) but not mirroring implementation SOLID - DRY
  6. Data
  7. Data
  8. Builders/Data DRY Can build graphs
  9. Builders
  10. Builders
  11. No unreliable resources file system/network No dates/random numbers
  12. Triangulation
  13. What’s this for?
  14. Single behaviour CHeck back to naming Executable Documentation
  15. No unreliable resources file system/network No dates/random numbers
  16. Osherove - maintainable, trustworthy, readable Bache - coverage, readability, robustness, speed
  17. TDD. Decoupling.