How to Narrow Down What to Test by Zsolt Fabok ...
“I get paid for code that works, not for tests, so myphilosophy is to test as little as possible to reach a givenlevel of ...
I’d like to [re]start working on this old application
My “where to start” list
#1 Determine which parts of the code are really used
Always Often 45% 7% Sometimes ...
The goal is to find those featureswhich are always or often used By studying coverage, access logs, traces...
Let’s have a look at the coverage(using instrumented class files):% cp jetty/cobertura.ser web.ser% cp uploader/cobertura.s...
Example #1: overview
Example #2: execution not even executed
Example #3: number of execution .NET wins
FileBasedVerifier (usage)FileHelper (usage)
#2 Find out which parts of the code change often
By checking how many times a file hasbeen committed into VCS:% ./git_stat.sh14, VerifierTask.java13, index.jsp11, FileBasedU...
FileBasedVerifier (usage)FileHelper (usage)VerifierTask (changes)index.jsp (changes)FileBasedUserHome (changes)
#3 Determine which part of the code changes data
Code review“You have exactly 1 minute to explain to mewhat that method does!”
FileBasedVerifier (usage)FileHelper (usage, review)VerifierTask (changes)index.jsp (changes)FileBasedUserHome (changes, re...
#4 Determine where the code is most likely going to fail
Static code checkers http://www.aucklandpestcontrol.net.nz/
PMD is not helpful at the moment, but good to know
FileBasedVerifier (usage)FileHelper (usage, review, bugs)VerifierTask (changes)index.jsp (changes)FileBasedUserHome (chang...
Let’s order our list and we are done!
FileHelper (usage, review, bugs)FileBasedVerifier (usage)FileBasedUserHome (changes, review)VerifierTask (changes) Verifi...
Now we know where to start, but we’re not sure how?
Before we continue, let’s talk about the “mythical 80% test coverage”...
Gaining 30% coverage in 2 minutes:public class CheaterTest { @Test public void shouldIncreaseTheCoverage() { Harvest...
So, you start with an assertion:public class FileHelperTest { @Test public void shouldReturnTheContentOfAFile() throws I...
First test case is done:public class FileHelperTest { @Test public void shouldReturnTheContentOfAFile() throws IOExcepti...
Now we have two test cases:public class FileHelperTest { @Test public void shouldReturnTheContentOfAFile() throws IOExce...
And we are done (assertion + coverage):public class FileHelperTest { private File input; @Before public void setUp(...
Well tested codeWell tested code everywhere
Thank you very much for your attention!http://zsoltfabok.com/ @ZsoltFabok
Narrow Down What to Test
Narrow Down What to Test
of 35

Narrow Down What to Test

Agile Cambridge 2012
Published on: Mar 3, 2016
Published in: Technology      
Source: www.slideshare.net


Transcripts - Narrow Down What to Test

  • 1. How to Narrow Down What to Test by Zsolt Fabok 2012.09.28 Not sure if I have to check out this problem Or just wait for the customer feedback@ZsoltFabok #agilecamhttp://zsoltfabok.com/ http://www.agilecambridge.net/ac2012/
  • 2. “I get paid for code that works, not for tests, so myphilosophy is to test as little as possible to reach a givenlevel of confidence (I suspect this level of confidence ishigh compared to industry standards, but that could justbe hubris). If I don’t typically make a kind of mistake (likesetting the wrong variables in a constructor), I don’t testfor it. I do tend to make sense of test errors, so I’m extracareful when I have logic with complicated conditionals.When coding on a team, I modify my strategy to carefullytest code that we, collectively, tend to get wrong.” Kent Beck - September 10, 2010 http://stackoverflow.com/questions/153234/how-deep-are-your-unit-tests/153565#153565
  • 3. I’d like to [re]start working on this old application
  • 4. My “where to start” list
  • 5. #1 Determine which parts of the code are really used
  • 6. Always Often 45% 7% Sometimes Rarely Never 13% 19% 16%The good old Standish Group Study
  • 7. The goal is to find those featureswhich are always or often used By studying coverage, access logs, traces, web analytics, heat maps, etc.
  • 8. Let’s have a look at the coverage(using instrumented class files):% cp jetty/cobertura.ser web.ser% cp uploader/cobertura.ser ant.ser% ant usage_coverageusage_coverage:[cobertura-merge] Cobertura: Loaded information on 12 classes.[cobertura-merge] Cobertura: Loaded information on 11 classes.[cobertura-merge] Cobertura: Saved information on 16 classes.[cobertura-report] Cobertura: Loaded information on 16 classes.[cobertura-report] Report time: 600msBUILD SUCCESSFULTotal time: 2 seconds
  • 9. Example #1: overview
  • 10. Example #2: execution not even executed
  • 11. Example #3: number of execution .NET wins
  • 12. FileBasedVerifier (usage)FileHelper (usage)
  • 13. #2 Find out which parts of the code change often
  • 14. By checking how many times a file hasbeen committed into VCS:% ./git_stat.sh14, VerifierTask.java13, index.jsp11, FileBasedUserHome.java11, FileBasedUser.java11, FileBasedContentTracker.java 8, IntegrityCheckTask.java 7, MailSender.java
  • 15. FileBasedVerifier (usage)FileHelper (usage)VerifierTask (changes)index.jsp (changes)FileBasedUserHome (changes)
  • 16. #3 Determine which part of the code changes data
  • 17. Code review“You have exactly 1 minute to explain to mewhat that method does!”
  • 18. FileBasedVerifier (usage)FileHelper (usage, review)VerifierTask (changes)index.jsp (changes)FileBasedUserHome (changes, review)FileBasedContentTracker (review)
  • 19. #4 Determine where the code is most likely going to fail
  • 20. Static code checkers http://www.aucklandpestcontrol.net.nz/
  • 21. PMD is not helpful at the moment, but good to know
  • 22. FileBasedVerifier (usage)FileHelper (usage, review, bugs)VerifierTask (changes)index.jsp (changes)FileBasedUserHome (changes, review)FileBasedContentTracker (review, bugs)FileBasedContentTracker.fsck() (crap4j)FileBasedContentTracker.gc() (crap4j)HarversterTask (bugs)VerifierTask.execute() (crap4j)
  • 23. Let’s order our list and we are done!
  • 24. FileHelper (usage, review, bugs)FileBasedVerifier (usage)FileBasedUserHome (changes, review)VerifierTask (changes) VerifierTask.execute() (crap4j)FileBasedContentTracker (review, bugs) FileBasedContentTracker.fsck() (crap4j) FileBasedContentTracker.gc() (crap4j)index.jsp (changes)HarversterTask (bugs)
  • 25. Now we know where to start, but we’re not sure how?
  • 26. Before we continue, let’s talk about the “mythical 80% test coverage”...
  • 27. Gaining 30% coverage in 2 minutes:public class CheaterTest { @Test public void shouldIncreaseTheCoverage() { HarvesterTask harvester = new HarvesterTask(); Project project = new Project(); project.setBaseDir(new File(".")); harvester.setProject(project); harvester.setRepository("../repository"); harvester.setHistory("history"); harvester.setTemplate("templates"); harvester.execute(); }}Covered code != Tested code
  • 28. So, you start with an assertion:public class FileHelperTest { @Test public void shouldReturnTheContentOfAFile() throws IOException { assertEquals("", FileHelper.getFileContent(null)); }}➡ The ‘assertEquals’ makes sure that your test actuallydoes something➡ The ‘null’ parameter - along with theNullPointerException - will show you where to continue
  • 29. First test case is done:public class FileHelperTest { @Test public void shouldReturnTheContentOfAFile() throws IOException { File input = File.createTempFile("foo", "bar"); assertEquals("", FileHelper.getFileContent(input)); }}➡ Now the test is green, let’s continue with a moremeaningful test case
  • 30. Now we have two test cases:public class FileHelperTest { @Test public void shouldReturnTheContentOfAFile() throws IOException { File input = File.createTempFile("foo", "bar"); assertEquals("", FileHelper.getFileContent(input)); } @Test public void shouldReturnTheContentOfAFile() throws IOException { File input = File.createTempFile("foo", "bar"); new FileOutputStream(input).write("something".getBytes()); assertEquals("something", FileHelper.getFileContent(input)); }}➡ Test method names remains the same until the body isfilled properly
  • 31. And we are done (assertion + coverage):public class FileHelperTest { private File input; @Before public void setUp() throws IOException { input = File.createTempFile("foo", "bar"); } @Test public void shouldReturnAnEmptyStringForAnEmptyFile() throws IOException { assertEquals("", FileHelper.getFileContent(input)); } @Test public void shouldReturnTheContentOfAFile() throws IOException { setInputFileContent("something"); assertEquals("something", FileHelper.getFileContent(input)); } private void setInputFileContent(String content) throws IOException { new FileOutputStream(input).write("something".getBytes()); }}
  • 32. Well tested codeWell tested code everywhere
  • 33. Thank you very much for your attention!http://zsoltfabok.com/ @ZsoltFabok

Related Documents