In this session, we'll learn what TDD and BDD testing is in CFML and ColdFusion, using Testbox. We'll look at some of the myths surrounding testing, and how to get started with testing, and how it can make you more efficient, improve code quality, code confidence, and how automation can make your life as a developer more pleasant than what you face currently, living in the trenches
BDD Testing and Automating from the trenches - Presented at Into The Box June 14, 2016
1. BDD Testing & Automation
from the Trenches Into the Box 2016
Gavin Pickin
2. ★Who Am I?
★Gavin Pickin – developing Web Apps since late 90s
○Ortus Solutions Software Consultant
○ContentBox Evangelist
★What else do you need to know?
○CFMLRepo.com http://www.cfmlrepo.com
○Blog - http://www.gpickin.com
○Twitter – http://twitter.com/gpickin
○Github - https://github.com/gpickin
★Lets get on with the show.
3. ★State of the Room
★ A few questions for you guys
★ If you have arms, use them.
16. ★Integration Testing
★Integration Tests several of the pieces
together
★Most of the types of tests are variations of
an Integration Test
★Can include mocks but can full end to end
tests including DB / APIs
18. ★Unit Testing
“unit testing is a software verification
and validation method in which a
programmer tests if individual units of
source code are fit for use. A unit is the
smallest testable part of an application”
- wikipedia
19. ★Unit Testing
★Can improve code quality -> quick error
discovery
★Code confidence via immediate verification
★Can expose high coupling
★Will encourage refactoring to produce >
testable code
★Remember: Testing is all about behavior and
expectations
20. ★Styles – TDD vs BDD
★TDD = Test Driven Development
○Write Tests
○Run them and they Fail
○Write Functions to Fulfill the Tests
○Tests should pass
○Refactor in confidence
★Test focus on Functionality
21. ★Styles – TDD vs BDD
★BDD = Behavior Driven Development
Actually similar to TDD except:
○Focuses on Behavior and Specifications
○Specs (tests) are fluent and readable
○Readability makes them great for all levels of
testing in the organization
★Hard to find TDD examples in JS that are not
using BDD describe and it blocks
22. ★TDD Example
Test( ‘Email address must not be blank’,
function(){
notEqual(email, “”, "failed");
});
23. ★BDD Example
Describe( ‘Email Address’, function(){
It(‘should not be blank’, function(){
expect(email).not.toBe(“”);
});
});
28. ★TestBox
TestBox is a next generation testing framework for
ColdFusion (CFML) that is based on BDD (Behavior Driven
Development) for providing a clean obvious syntax for
writing tests.
It contains not only a testing framework, runner,
assertions and expectations library but also ships with
MockBox, A Mocking & Stubbing Framework,.
It also supports xUnit style of testing and MXUnit
compatibilities.
30. ★TestBox BDD Example
describe("Hello world function", function() {
it(”contains the word world", function() {
expect(helloWorld()).toContain("world");
});
});
31. ★TestBox New BDD Example
feature( "Box Size", function(){
describe( "In order to know what size box I need
As a distribution manager
I want to know the volume of the box", function(){
scenario( "Get box volume", function(){
given( "I have entered a width of 20
And a height of 30
And a depth of 40", function(){
when( "I run the calculation", function(){
then( "the result should be 24000", function(){
// call the method with the arguments and test the outcome
expect( myObject.myFunction(20,30,40) ).toBe( 24000 );
});
});
});
32. ★Using Testing in your
Workflow
★Using HTML Test Runners
○Keep a Browser open
○F5 refresh tests
33. ★Command Line Tests
★Run testbox – manual
○Run tests at the end of each section of work
★Run Grunt-Watch – automatic
○Runs testbox on every file change
○Grunt can run other tasks as well,
minification etc
34. ★Testing in your IDE
★Browser Views
○Eclipse allows you to open files in web view –
uses HTML Runner
★Run testbox/ Grunt / in IDE Console
○Fairly Easy to setup
○See Demo– Sublime Text 3 (if we have time)
36. ★Create a runner.cfm
<cfsetting showDebugOutput="false">
<!--- Executes all tests in the 'specs' folder with simple reporter by
default --->
<cfparam name="url.reporter" default="simple">
<cfparam name="url.directory" default="tests.specs">
<cfparam name="url.recurse" default="true" type="boolean">
<cfparam name="url.bundles" default="">
<cfparam name="url.labels" default="">
<!--- Include the TestBox HTML Runner --->
<cfinclude template="/testbox/system/runners/HTMLRunner.cfm" >
37. ★Create a Test Suite
// tests/specs/CFCTest.cfc
component extends="testbox.system.BaseSpec" {
function run() {
it( "will error with incorrect login", function(){
var oTest = new cfcs.userServiceRemote();
expect( oTest.login( 'gavin@gavin.com',
'topsecret').result ).toBe('400');
});
}
}
38. ★Create a 2nd
Test Suite
// tests/specs/APITest.cfc
component extends="testbox.system.BaseSpec" {
function run() {
describe("userService API Login", function(){
it( "will error with incorrect login", function(){
var email = "gavin@gavin.com";
var password = "topsecret”;
var result = "";
http url="http://127.0.0.1:8504/cfcs/userServiceRemote.cfc?
method=login&email=#email#&password=#password#" result="result”;
expect( DeserializeJSON(result.filecontent).result ).toBe('400');
});
});
}
}
47. ★Testbox Runner JSON
★Testbox has several runners, you have seen the
HTML one, this Runner uses the JSON runner and
then formats it.
★http://127.0.0.1:53874/tests/runner.cfm?
reporter=json