Codemash 2013 – Getting Started With SpecFlow( with some BDD and ATDD thrown in )

presenter James Bender, VP of Technology Improving Enterprises www.ImprovingEnterprises.com.

3:35  PM  1/10/2013 in Portia/Wisteria

James comes from a full on Agile shop, very into TDD.  Indicating testing very important for dynamic languages, like javascript!

Reviewed benefits of TDD: Quality, documentation in tests, more focused on what I’m doing since have to break down tasks into smaller tasks, reduces rust code.  Code doesn’t exist if no business test exists that uses it.  Less bugs.  Bugs won’t come back since test exists that proves its gone.

Behavior Driven Development(BDD)

Acceptance Test Driven Development(ATDD):   Get acceptance tests, write code to make them pass to implement.

Mentioned that he does not believe that there is right way to do this.  Just ignore people who say you should do it this way and do whatever works best for you.

Started doing actual BDD for Tic Tac Toe as excercise using SpecFlow as unit testing framework and taking a BDD approach.  Used nuGet to get Specflow using Nunit.  Create project called TicTacToe.Specs.  Need to go to SpecFlows website and install some software for processing BDD tests.

SpecFlow is basically a code generator that creates unit test code from SpecFlow Gerkin based Feature file.

Working With SpecFlow

  1. Add a Feature file.  This allows english language gerkin syntax driven tests.  Feature, Scenario, Given Then When syntax.  The feature file and gerkin syntax has no dependency on platform or language so can use it on any platform.  ( Can use Gerkin based Feature definition as your user story also if you want.  Then your user stories can become actual tests)
  2. Generate test files.   Specflow can goof up generating files, if this happens just use Run Custom Tool option to get it to regenerate.
  3. Create a step for each Gerkin statement.  Each Gerkin statement will become a unit test in generated tests.  Specflow wraps test attributes with its own Given and When attributes so abstracts from particular test framework.  Passing tests are marked ‘done’ when run tests in specFlow.

We as a group then went through the process of creating specFlow scenarios for the Feature GameWinner in a Tic Tac Toe game.  You can parameterize specFlow tests by using regular experssions.  This allows scenarios that are different only in value will be matched by steps that are defined with regular expressions.

In SpecFlow can write a given that visually defines  a table that then passes a Table to the unit test.  Table is collection of TableRows which are just keyvalue pairs where key is the table column name.

Quickly reviewed Mocking and Dependency Injection as concepts that underpin SpecFlow and BDD.

How do you get your management to buy into specFlow and TDD.  What do you tell them to convince them to do this.  Money and time.  They want to know how long will this take and how much will it cost.  Showed cost of fixing defect based on when it’s found chart.  Much more expensive to find defects later in process.  BDD and SpecFlow aid in finding bugs sooner and therefore save us money!  Showed a Defect Density and time to delivery chart.  Longer projects had much lower defect density, but not that much longer and had a lot fewer defects.  QA people should be aware this technology will not replace them but allow them to focus on things that only a human can do.

 

 

Codemash 2013 Keynote Neal Ford

After three years I’ve returned to Codemash! Just like three years ago I’m going to post my notes here so to help the learning sink in!

The keynote presentation started a little late, around 8:20 Am.  I was a little bleary eyed after my hour long drive out from Cleveland, and had a bit of a food coma going after a super quick breakfast.  All in all I was hoping to have an eye popping and life altering experience, or at least enough to keep me from passing out in my scrambled eggs.   I didn’t pass out, but I didn’t have any religious experiences either.

The keynote was given by Neal Ford.  See his website for the details but to summarize author and big wheel at ThoughtWorks.  Apparently has written a few books and has a new book out relating design patterns to creating presentations.

Neal’s presentation was based on a concept called ‘Geek Leaking’.  The basic concept was applying problem solving from disparate areas to each other to come up with unanticipated breakthroughs.    For example apply the scientific method to anything to break down walls.

The presentation began with definitions of the word “Geek’.  Apparently Geek is a verb as well as a noun,  ‘Geeking’ is spending inordinate amounts of time on disciplines that other people don’t find very exciting.

Richard Feynman worked at Los Alamos and had a deep physics background.  He was known for applying his problem solving skills to areas well outside of his specialty and achieving impressive results.

Forbes published an article about how all companies now have some software aspect to them.  In this regard all companies are in some way software companies and the ‘Geekness’ of software professionals is now leaking into normal businesses.

Next Neal’s book ‘Presentation Patterns’ was used as an example of how the ‘Geekness’ of software people is now leaking into presentations.  Neal gave some examples of some good and bad presentation formatting.  Anti patterns in presentations:  looking old even if using new technology, hiccup words, Alienating artifacts.  Good patterns in presentations: Context Keepers, Backtracking, Blank Slide to put focus on you when you want it.

Reviewed John McCarthy from Stanford in fifties, was creator of LISP language ran AI lab.  Neal put forth that many of personal computer researchers at that time were hippies, and that hippies were more visionary than normal people so the PC age was ushered in.   One problem, however, hippies are egalitarian so couldn’t forsee email being abused for spam.

Neal admitted to making  crank calls as a child.  He indicated he has stopped this now that caller id has come into use.  No fun making crank call when recipient knows where you live.  Not quite sure what the point here was, but it’s interesting.

Richard Feynman did work optimizing process at a Uranium Enrichment facility.  Part of this work laid the groundwork for parallel computing.  Feynman essentially created a human parallel computing process to help with large calculations he was needing to perform.

Neal next mentioned the book Power of Habit.  Habituation causes the brain to be able to do tasks with less effort.  Once a task is a habit brain can accomplish it with far less mental effort.

The presentation now spent a good amount of time detailing benefits of continuous integration and deployment.  Mentioned anecdote about Martin Fowler as a teenager in Birmingham UK visiting a software project that was bogged down in long ‘integration’ process of software that was written then ‘integrated’.  Used this as a reason to integrate every day to make integration less painful by doing it more often.  ‘Bring the pain forward’ to make the pain less.

Deming Cycle is really just scientific method and really is Geek Leakage from research and statistics into software.

Richard Feynman like pretending to be a safe cracker.  He knew many safe codes were birthdays so would sneak into people’s offices and spend time guessing their safe codes, then if front of them would pretend he could crack their safe.

Jeweler’s Hammer metaphor.

Use a jeweler’s hammer to break a large problem domain down into two or more more easily solved problem domains.  Neal used time as  a hammer to break down presentation problems, gave examples of Traveling Highlight and code Overlays as examples of how looking for how to save time lead to breakthroughs.  Also used clojure as an example of a jeweler’s hammer approach to solve variable threading problems in Java.

Evolutionary Architecture and Emergent Design.  Architecture is anything that is really hard to change.  Architecture should start small and evolve.  Design is for things that are flexible so it can emerge as a project moves forward since changing it has minor consequences as compared to architecture changes.

Continuous Integration for Components.  Can create systems that self update when a dependency changes all the way through deploying a production ready version.  Guarded dependency is build with an updated component that did not make it all the way to deployable code.  Trust is required to setup systems that automatically consume other component updates.  Mentioned Github and change in open source motif where there are now pullers who get source code and make changes and then submit them versus old school allowed collaborators on a project.

Richard Feynman played the bongos.

Downsides to Geek Leakage: overly complicated solutions.  Reference wired article about advanced math used on Wall street being reason for financial melt down.  I disagree, if banks were allowed to fail they would not use these tool irresponsibly, it is the lack of risk involved since banks are too big to fail that lets them run massive risks that then damage the economy.

Neal then reviewed his concerns regarding armed robots.  He wants to make sure that armed not so smart robots have rock solid code.  In this context Neal mentioned Asimov’s robot books and the robot laws as  a good idea.

Richard Feynman participated in reviewing the challenger disaster.  By using a simple approach was more effective than statisticians  He simply put o-ring material in cold water to show it was ridgid.

In short, embrace scientific method everywhere, use Jeweler’s hammers and only Geek Leak positively.

Codemash 2013 – Advanced Unit Testing Considering The Stakeholder

Presenter Peter Ritchie – Ottowa Canada peterritchie.com.

1:45 PM  1/10/2013 in Portia/Wisteria

My Overview

Interesting perspective as far as generating automated acceptance tests.  Good idea to write out acceptance tests in english using a BDD style Given, Then When syntax.  Doing this you can involve stakeholders in the acceptance test creation process.  Can then convert these english language tests to test code that now has meaning to the stakeholder.  To create documentation can write a tool to build a nice html report that is in a BDD format and lets stakeholder clearly see the results of tests they helped write and understand more thoroughly.

This is a great idea for acceptance testing.  It ignores the difficulty of doing this in systems that have a lot of dependencies that can’t be run real time for testing.  I see this is more a companion to developer unit testing than a replacement.  More importantly could be very handy at more thoroughly ferreting out requirements by having stakeholders help write acceptance tests in syntax they can understand.

My Notes

Agenda

  • Principles
  • Readability
  • Maintainability
  • Documentation
Principles
What is a unit test?
  • Automated
    Just an automated test as far as Peter is concerned.  A unit being tested is not meaningful to a stakeholder.   Acceptance testing is  what matters to stakeholders.
Why
  • Verify changes to code
Good Unit Tests
  • Automatable
  • Trustworthy
    a failure is meaningful and can actually fail
  • Readable
  • Maintainable
    don’t want to have to change unit tests every time you change any code
Going to focus on readable and maintainable unit testing.
Now he started walking through code examples:
  • Simple Calculate Class that adds two numbers
    three asserts in one test method which makes it unclear why the test failed when it fails.  Make smaller tests, with one actual test and a human readable name so failing test name tells you all you need to know.
  • Reviewed Arrange, Act Assert methodology as means for organizing a test
    Arrange test data, the Act by running method under test, then assert to validate test worked.
  • Refactors his tests to make tests more readable and to reduce duplicated code
    Setup and Teardown methods – reviewed that setup and teardown run before and after each test in the test class.
    If multiple tests are exact same code with just different parameters then can pass in rows of data to one test to just change parameters
BankAccount Demo
  • Used BankAccount class as example class to test to show more business logic complexity
  • Using BDD (Behavioral Driven Design) as mechanism for showing how to structure acceptance testing
    really downplayed the notion of mocking.   Can only do this testing if have whole system available
  • Acceptance Criteria
    Given an initial state, after a certain action, ensure that the post state is correct.  Good to have users write out acceptance tests by hand in Given, then when syntax.  Gerkin format.
  • Walked throw an example of updating class and test naming.   Class name now ‘when_depositing_to_active_account_through_atm.
    Uses ‘When’ clause to name files, then use ‘then’ clauses to name tests in the file.  Makes for a logical means of partitioning tests into smaller files that makes sense to the stakeholder.
  • Showed resharpers test runner, as far as how it shows nested view which may be more meaningful to the stakeholder
  • The Given is the setup of the test class
  • Showed example of using base class to share setup methods across multiple test fixtures
  • If you use a Given, When Then syntax when writing tests, as in having stakeholders write them, can use the above approach to convert them to acceptance test code that will then be much more meaningful to the stakeholder.
Fluent Interface
  • Walked threw example of a Fluent API for building a type so it is clear what every value that is being set is.
  •   new BankAccountBuilder().OfType(AccountType.Savings)
    .WithBalance(500m)
    .WithId(1);
  • Builder is only test code used for testing, not used in production code.
  • Fluent is method that sets property on instance that returns the instance to allow  chaining
  • public BankAccount WithId(int id)
    {
    this.Id = id;
    return this;
    }
  • Can use tools to run Given When Then structured tests to create report output that is very meaningful to stake holders.
    Scenario is ‘When’
    Given is Setup
    When are individual tests.
    DocumentationGenerator can be run to create acceptance test output
  • Walked through his implementation of DocumentationGenerator that runs tests and then builds nice html Scenario, Given Then When report.  Good idea.
    Basically gets test-fixtures from assembly finding them by attribute [TestFixture], sets up html header for fixture, runs tests creates html for each test, then writes end html for fixture and moves on!