Unit Testing with MxUnit
description
Transcript of Unit Testing with MxUnit
![Page 1: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/1.jpg)
Jason Michl
![Page 2: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/2.jpg)
Installation of MxUnitIntroduction to Unit TestingExample ApplicationSimple Unit TestsTest SuitesComplex Unit Tests
![Page 3: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/3.jpg)
http://www.mxunit.org/doc/index.cfm?doc=installframework
Summary:Download.Unzip to root folder of website.Test: http://localhost/mxunit/index.cfm
![Page 4: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/4.jpg)
Installation of MxUnitIntroduction to Unit TestingExample ApplicationSimple Unit TestsTest SuitesComplex Unit Tests
![Page 5: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/5.jpg)
Unit testing is a means to improve code quality and reliability.
Individual units of code are tested according to specifications.
Defects can be identified before integration with other code.
![Page 6: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/6.jpg)
Test-Driven DevelopmentWrite a test that failsWrite code that causes the test to passRepeat until code meets specifications
![Page 7: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/7.jpg)
Asserts evaluate statements, and cause unit tests to fail when certain conditions are not met.Example:
AssertTrue(x eq y);If x equals y, then continue with the test.If x does not equal y, then the test fails.
![Page 8: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/8.jpg)
Built-in asserts(Reminder: Assert functions fail when
their condition fails.)AssertTrue, AssertFalse, assertEquals,assertSame, assertNotSame
Full list:http://wiki.mxunit.org/display/default/Built-
In+Assertions
![Page 9: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/9.jpg)
AssertTrue: Fails when condition is false.AssertTrue: Fails when condition is true.AssertEquals: Fails when arguments are not equal.AssertSame: Fails when arguments are not the same instance. (Arrays always fail.)AssertNotSame: Fails when arguments are the same instance. (Arrays always pass.)
![Page 10: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/10.jpg)
MxUnit tests are written as components.Test components extend existing MxUnit components, and test functions through
asserts.
![Page 11: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/11.jpg)
Example syntax:<cfcomponent displayname="testProjects" extends="mxunit.framework.TestCase">
<cffunction name="A_getProjectList_Exists" access="remote" returntype="void">
<cfscript>ProjectGateway = createObject("component", "components.project.ProjectGateway");assertTrue(isQuery(ProjectGateway.getProjectList()));
</cfscript> </cffunction>
</cfcomponent>
![Page 12: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/12.jpg)
Installation of MxUnitIntroduction to Unit TestingExample ApplicationSimple Unit TestsTest SuitesComplex Unit Tests
![Page 13: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/13.jpg)
Example applicationUsed to demonstrate test-driven
developmentPractical framework to build unit tests
around“Real-world” goal: organize software
development projects
![Page 14: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/14.jpg)
TablesUsersUser TypesProjects
Team MembersNotes
(Note: Tables already have test data in them.)
![Page 15: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/15.jpg)
Requirements for “getProjectList”Return a list of valid project recordsAccept multiple optional arguments as
filters
![Page 16: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/16.jpg)
Basic tests for “getProjectList”Does function exist?Does function return a query?Does function return records from the
database?Does function accept and use
arguments?
![Page 17: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/17.jpg)
Basic tests for “getProjectList”Does function return zero records, when
no arguments have been passed?Does function return all records, when
the “all” argument is passed?
![Page 18: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/18.jpg)
Installation of MxUnitIntroduction to Unit TestingExample ApplicationSimple Unit TestsTest SuitesComplex Unit Tests
![Page 19: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/19.jpg)
<cffunction name="A_getProjectList_Exists" access="remote" returntype="void"><cfscript>projectGatewayDemo = createObject("component", "components.project.projectGatewayDemo");assertTrue(isQuery(projectGatewayDemo.getProjectList()));</cfscript> </cffunction>
<cffunction name="getProjectList" access="public" returntype="query" output="false"></cffunction>
![Page 20: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/20.jpg)
<cffunction name="A_getProjectList_Exists" access="remote" returntype="void"><cfscript>projectGatewayDemo = createObject("component", "components.project.projectGatewayDemo");assertTrue(isQuery(projectGatewayDemo.getProjectList()));</cfscript> </cffunction>
<cffunction name="getProjectList" access="public" returntype="query" output="false"><cfset var getProjectList = queryNew("empty") /><cfreturn getProjectList /></cffunction>
![Page 21: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/21.jpg)
<cffunction name="C_getProjectList_Returns_Records_Passes" access="remote" returntype="void"><cfscript>projectGatewayDemo = createObject("component", "components.project.projectGatewayDemo");assertTrue((projectGatewayDemo.getProjectList().recordCount gt 0), "No records were returned.");</cfscript> </cffunction>
<cffunction name="getProjectList" access="public" returntype="query" output="false"><cfset var getProjectList = queryNew("empty") /><cfreturn getProjectList /></cffunction>
![Page 22: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/22.jpg)
<cffunction name="C_getProjectList_Returns_Records_Passes" access="remote" returntype="void"><cfscript>projectGatewayDemo = createObject("component", "components.project.projectGatewayDemo");assertTrue((projectGatewayDemo.getProjectList().recordCount gt 0), "No records were returned.");</cfscript> </cffunction>
<cffunction name="getProjectList" access="public" returntype="query" output="false"><cfset var getProjectList = "" /><cfquery name="getProjectList" datasource="bugTrack">
SELECT *FROM prProjects
</cfquery><cfreturn getProjectList /></cffunction>
![Page 23: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/23.jpg)
<cffunction name="D_getProjectList_Passes_createdBy_Passes" access="remote" returntype="void"><cfset projectGatewayDemo = createObject("component", "components.project.projectGatewayDemo") /><cfset getProjectList = projectGatewayDemo.getProjectList(createdBy = 1) /><cfloop query="getProjectList"><cfset assertTrue(getProjectList.createdBy eq 1, "Projects were returned with the wrong creator.") /></cfloop></cffunction>
<cffunction name="getProjectList" access="public" returntype="query" output="false"><cfset var getProjectList = "" /><cfquery name="getProjectList" datasource="bugTrack">
SELECT *FROM prProjects
</cfquery><cfreturn getProjectList /></cffunction>
![Page 24: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/24.jpg)
<cffunction name="D_getProjectList_Passes_createdBy_Passes" access="remote" returntype="void"><cfset projectGatewayDemo = createObject("component", "components.project.projectGatewayDemo") /><cfset getProjectList = projectGatewayDemo.getProjectList(createdBy = 1) /><cfloop query="getProjectList"><cfset assertTrue(getProjectList.createdBy eq 1, "Projects were returned with the wrong creator.") /></cfloop></cffunction>
<cffunction name="getProjectList" access="public" returntype="query" output="false"><cfargument name="createdBy" default="" /><cfset var getProjectList = "" /><cfquery name="getProjectList" datasource="bugTrack">
SELECT *FROM prProjectsWHERE 0 = 0
<cfif len(arguments.createdBy)>AND createdBy = <cfqueryparam cfsqltype="cf_sql_integer"
value="#val(arguments.createdBy)#" /></cfif></cfquery><cfreturn getProjectList /></cffunction>
![Page 25: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/25.jpg)
<cffunction name="G_getProjectList_Returns_Zero_Records_Passes" access="remote" returntype="void"><cfset projectGatewayDemo = createObject("component", "components.project.projectGatewayDemo") /><cfset getProjectList = projectGatewayDemo.getProjectList() /><cfset assertTrue(getProjectList.recordCount eq 0, "No arguments were sent. Query should have returned 0 results.") /></cffunction>
<cffunction name="getProjectList" access="public" returntype="query" output="false"><cfargument name="createdBy" default="" /><cfset var getProjectList = "" /><cfquery name="getProjectList" datasource="bugTrack">
SELECT *FROM prProjectsWHERE 0 = 0
<cfif len(arguments.createdBy)>AND createdBy = <cfqueryparam cfsqltype="cf_sql_integer"
value="#val(arguments.createdBy)#" /></cfif></cfquery><cfreturn getProjectList /></cffunction>
![Page 26: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/26.jpg)
<cffunction name="G_getProjectList_Returns_Zero_Records_Passes" access="remote" returntype="void"><cfset projectGatewayDemo = createObject("component", "components.project.projectGatewayDemo") /><cfset getProjectList = projectGatewayDemo.getProjectList() /><cfset assertTrue(getProjectList.recordCount eq 0, "No arguments were sent. Query should have returned 0 results.") /></cffunction>
<cffunction name="getProjectList" access="public" returntype="query" output="false"><cfargument name="createdBy" default="" /><cfset var getProjectList = "" /><cfquery name="getProjectList" datasource="bugTrack">
SELECT *FROM prProjectsWHERE
<cfif len(arguments.createdBy) OR len(arguments.approvalStatus) OR len(arguments.projectID)>0 = 0
<cfelse>0 = 1
</cfif><cfif len(arguments.createdBy)>
AND createdBy = <cfqueryparam cfsqltype="cf_sql_integer" value="#val(arguments.createdBy)#" /></cfif></cfquery><cfreturn getProjectList /></cffunction>
![Page 27: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/27.jpg)
<cffunction name="H_getProjectList_Returns_All_Records_Passes" access="remote" returntype="void">
<cfset projectGatewayDemo = createObject("component", "components.project.projectGatewayDemo") /><cfset getProjectList = projectGatewayDemo.getProjectList(all = 1) /><cfset assertTrue(getProjectList.recordCount gt 0, "The 'all' argument was sent. Query should have returned results.") /></cffunction>
<cffunction name="getProjectList" access="public" returntype="query" output="false"><cfargument name="createdBy" default="" /><cfset var getProjectList = "" /><cfquery name="getProjectList" datasource="bugTrack">
SELECT *FROM prProjectsWHERE
<cfif len(arguments.createdBy) OR len(arguments.approvalStatus) OR len(arguments.projectID)>0 = 0
<cfelse>0 = 1
</cfif><cfif len(arguments.createdBy)>
AND createdBy = <cfqueryparam cfsqltype="cf_sql_integer" value="#val(arguments.createdBy)#" /></cfif></cfquery><cfreturn getProjectList /></cffunction>
![Page 28: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/28.jpg)
<cffunction name="H_getProjectList_Returns_All_Records_Passes" access="remote" returntype="void">
<cfset projectGatewayDemo = createObject("component", "components.project.projectGatewayDemo") /><cfset getProjectList = projectGatewayDemo.getProjectList(all = 1) /><cfset assertTrue(getProjectList.recordCount gt 0, "The 'all' argument was sent. Query should have returned results.") /></cffunction>
<cffunction name="getProjectList" access="public" returntype="query" output="false"><cfargument name="createdBy" default="" /><cfargument name="all" default="" /><cfset var getProjectList = "" /><cfquery name="getProjectList" datasource="bugTrack">
SELECT *FROM prProjectsWHERE
<cfif len(arguments.createdBy) OR len(arguments.approvalStatus) OR len(arguments.projectID) OR len(arguments.all)>
0 = 0<cfelse>
0 = 1</cfif><cfif len(arguments.createdBy)>
AND createdBy = <cfqueryparam cfsqltype="cf_sql_integer" value="#val(arguments.createdBy)#" /></cfif></cfquery><cfreturn getProjectList /></cffunction>
![Page 29: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/29.jpg)
Example:http://127.0.0.1/mxunit/testCollection/unitTests/projectGatewayTestDemo.cfc?flush=&method=runTestRemote&event=home&output=ht
ml (shows each iteration of the development
cycle)
![Page 30: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/30.jpg)
Example:http://127.0.0.1/mxunit/testCollection/unitTests/projectGatewayTest.cfc?flush=&method=runTestRemote&event=home&output=html
(shows final version of getProjectList)
![Page 31: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/31.jpg)
Installation of MxUnitIntroduction to Unit TestingExample ApplicationSimple Unit TestsTest SuitesComplex Unit Tests
![Page 32: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/32.jpg)
Tests can be stored in multiple files and accessed as needed.The “getProjectList” test is one of many tests available for BugTrack.
![Page 33: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/33.jpg)
Other test files cover:Getting project member listsGetting project notesGetting user listsGetting user type lists
![Page 34: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/34.jpg)
Test Suites allow you to run multiple test files at the same time.Example:
allGatewaySuite.cfm runs over 40 tests, across five files.
![Page 35: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/35.jpg)
Example syntax:<cfset testSuite = createObject("component","mxunit.framework.TestSuite").TestSuite()/>
<cfset testSuite.addAll("mxunit.testcollection.unittests.projectGatewayTest")/> <cfset testSuite.addAll("mxunit.testcollection.unittests.projectMemberGatewayTest")/> <cfset testSuite.addAll("mxunit.testcollection.unittests.projectNotesGatewayTest")/> <cfset testSuite.addAll("mxunit.testcollection.unittests.userGatewayTest")/> <cfset testSuite.addAll("mxunit.testcollection.unittests.userTypeGatewayTest")/>
<cfset results = testSuite.run()/> <cfset writeOutput(results.getResultsOutput('html'))/>
![Page 36: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/36.jpg)
(Show example suites in CFEclipse)
![Page 37: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/37.jpg)
Example:http://127.0.0.1/mxunit/testCollection/allGatewaySuite.cfm
![Page 38: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/38.jpg)
Installation of MxUnitIntroduction to Unit TestingExample ApplicationSimple Unit TestsTest SuitesComplex Unit Tests
![Page 39: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/39.jpg)
Test-driven development can be used to successfully test complex applications.Example: BugTrack needs to pull a project, team members, and notes.We have unit tests for each of these, and now we need to test all of them together.
![Page 40: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/40.jpg)
(Show examples in CFEclipse)
![Page 41: Unit Testing with MxUnit](https://reader036.fdocuments.net/reader036/viewer/2022062518/568145ae550346895db2a7ca/html5/thumbnails/41.jpg)