Showing posts with label tdd. Show all posts
Showing posts with label tdd. Show all posts

23 October 2006

Strong Typing vs. Strong Testing

[Blog on Blog]

... without a full set of unit tests (at the very least), you can't guarantee the correctness of a program. To claim that the strong, static type checking constraints in C++, Java, or C# will prevent you from writing broken programs is clearly an illusion (you know this from personal experience). In fact, what we need is

Strong testing, not strong typing.

points to note:

  1. A very concise code sample comparing strong type and weak type language, where weak type loose the semantic on checking variable type – it works as long as it implements the method expected.
  2. unit test is an extension to complier

31 August 2006

TDD web controls dev life cycle

An entire upside down experience to help coder focus on deliver 'good enough' requirements

In summary this dev pattern is

1. decide deliverables - the rendered html as browser user sees. (step 1, 2)
2. write ruby script to test the html (in fact it is reversed test the test script) (step 3)
3. act as web designer to use the web control to build a test page (of course, the web control is not existed yet) (step 4)
4. Implment web control (step 5)
5. test the web control (step 6-7)
6. change loop

A well designed website should be broken into three domains:
1) Reusable Web controls which render plain html
2) bare bone website composed of web controls. Pages are served as control containers and responsible for managing navigation, user experience. It should contains no inline css but class/id/name that allows UI designer change the 'skin' without touching the website, not even a downtime.
3) CSS

Here is the Test Driven Development Cycle
1. Agree with UI designer on html template. the static html prototype is the 'contract'.

2. break down the template into componentised controls that will be generated with user/custom controls. Save the html corresponding to the control to a new html e.g. ~\gallery\searchbox.html
<html>
<body>
<!--search box-->
<div id="PARENTDIV" name="PARENTDIV" class="TestGlobalStyleClass">
<label id="PARENTDIV.MessageLabel" name="PARENTDIV.MessageLabel">TestMessageLabelText</label><br>
<input type="text" id="PARENTDIV.SearchTerm" name="PARENTDIV.SearchTerm" class="SearchTermClass" size="100" maxlength="100">
<a href="http://test.webcontrols.com" id="PARENTDIV.AdvLink" name="PARENTDIV.AdvLink"
title="Test ToolTip" class="AdvLinkClass">Test Link Text</a>
<!--search box close--></div>
</body>
</html>

3. Write an Ruby script 'SearchBoxTestFixture.rb' to test this template. This is to validate our test case is sounds.

require 'watir'
require 'test/unit'
require 'test/unit/assertions'
include Watir

class SearchBoxTestFixture <>
def setup
localHostXSP = "http://127.0.0.1"
localHostIIS = "http://localhost/Talis.Web.Cenote.WebControls.Test"
@staticTemplate = 'http://localhost/Talis.Web.Cenote.WebControls.Test/static/searchbox.html'
remoteHost = "http://talis.com";
@testSite = localHostIIS
@ie = IE.new
end

def teardown
@ie.close
end

def test_allHtmlElementsExist
# page = "SearchBoxTest.aspx"
# @ie.goto(@testSite+'/'+page)

# test static html template
@ie.goto(@staticTemplate)

assert_equal(1, @ie.divs.length, 'Expecting only one div')
assert(@ie.div(:id, /PARENTDIV/).exists?, "Expecting div id 'PARENTDIV'")
assert(@ie.div(:name, /PARENTDIV/).exists?, "Expecting div name 'PARENTDIV'")

#assert message label
assert_equal(1, @ie.labels.length, 'Expecting only one lable box')
assert(@ie.label(:id, /PARENTDIV.MessageLabel/).exists?, "Expecting label id 'PARENTDIV.MessageLabel'")
assert(@ie.label(:name, /PARENTDIV.MessageLabel/).exists?, "Expecting label name 'PARENTDIV.MessageLabel'" )
assert('TestMessageLabelText', @ie.label(:id, "PARENTDIV.MessageLabel").innerText)

#assert input box
assert_equal(1, @ie.text_fields.length, 'Expecting only one input box')
assert(@ie.div(:id, /PARENTDIV/).text_field(:id, 'PARENTDIV.SearchTerm').enabled?, "Expecting textbox id 'PARENTDIV.SearchTerm'" )
assert(@ie.text_field(:name, 'PARENTDIV.SearchTerm').exists?, "Expecting textbox name'PARENTDIV.SearchTerm' textbox element")
assert_equal(100, @ie.text_field(:id, 'PARENTDIV.SearchTerm').size(), "Expecting textbox size 100")
assert_equal(100, @ie.text_field(:id, 'PARENTDIV.SearchTerm').maxLength(), "Expecting textbox maxLength 100")

#assert link
assert_equal(1, @ie.links.length, 'Expecting only one link')
assert(@ie.link(:id, 'PARENTDIV.AdvLink').exists?, "Expecting link id 'PARENTDIV.AdvLink'" )
assert(@ie.link(:name, /PARENTDIV.AdvLink/).exists?, "Expecting link name 'PARENTDIV.AdvLink'" )
assert_equal('http://test.webcontrols.com/', @ie.link(:name, /PARENTDIV.AdvLink/).href, "Expecting link url 'http://test.webcontrols.com'" )
assert(@ie.link(:title, 'Test ToolTip').exists?, "Expecting link title (tool tip) 'Test ToolTip'" )
assert_equal('Test Link Text', @ie.link(:id, 'PARENTDIV.AdvLink').innerText, "Expecting link text 'Test Link Text'" )

end
end

4. write SearchBoxTest.aspx that intend to render as to searchbox.html. Ours contains this:
<My:SearchBox
ID="PARENTDIV"
Class="TestGlobalStyleClass"
MessageLabel_Text="TestMessageLabelText"
MessageLabel_Class="TestMessageLabelClass"
SearchTerm_Class="SearchTermClass"
SearchTerm_Size="100"
AdvLink_Class="AdvLinkClass"
AdvLink_Tooltip="Test ToolTip"
AdvLink_InnerText="Test Link Text"
Runat="server"
/>

5. Implement the custom web control: SearchBox.cs

6. If haven't done so, write nUnit Testfixture to hook* ruby script into your test dll. So all tests will feedback as red/green light

7. Modify SearchBoxTestFixture.rb to target SearchBoxTest.rb.

Contract changed, start from 1. again

Notes:
1. You need to install Ruby WATIR
2. Travis Illig has this fatastic RubyTestExecutor integrates ruby and watir test scripts into nUnit test framework. Check his paper Integrated ASP.NET Web Application Testing with NUnit, Ruby, and Watir on code project.

26 August 2006

nAnt build script to test mono web app with Ruby and Watir

Download the build script template: myWebsite.build

1. Prove nant task <csc>, <nunit2> works/doesn't work with mono
build

  1. Prove test assembly is fine use vsnet (.net 1.1) build, reference *.net1.1*
    nunit.framework.dll , not the mono build.

    • build asm
    • tested with nUnit2.2.0 GUI exe
    • tested with nUnit-2.2.0-mono console exe.

    Both work as expected

  2. nAnt build. nant target mono-1.0; reference lib (nunit.framework.dll) set to NUnit-2.2.0-mono

    • test assembly with nUnit2.2.0 GUI exe. On loading asm it throws exception
      'This assembly was not built with the NUNIT framework and contains no tests'
      cases' on the popout message window
    • test assembly with nUnit-2.2.0-mono
      console exe. This returns:
      OS Version: Microsoft Windows NT 5.1.2600.0 .NET Version: 1.1.4322.2032 Tests run:
      0, Failures: 0, Not run: 0, Time: 0 seconds Open the assembly by .net
      reflector, surprise, surpise there is nothing in the dll
2. Use mono mcs compiler
  1. compiles the source
    mcs -r:system.dll -r:\sslocal\Cenote.root\dependencies\NUnit-
    2.2.0-mono\bin\nunit.framework.dll -t:library SampleRubyTestFixture.cs

  2. test - test assembly with nUnit2.2.0 GUI exe. - test assembly with
    nUnit-2.2.0-mono console exe. - test assembly with nUnit2.2.8 GUI exe. All work
    as expected

  3. Open the assembly by .net reflector, it confirms that our test case is in there This confirms that nUnit2.2.* GUI exe doesn't discriminate mono build, as long as nunit.framework.dll is the .net1.1 compliance version This also confirms that the problem is not with nant task <nUnit2>, regardless which nunit build version it points to.
3. Questioning nant task <csc>
Before setting off to use task <exec> and mcs for the build, I have a few more go on task <csc>. Eventually it confirms that it was a bug in the <csc> task causes the problem.
nAnt doesn't support multiple filesets or its derived types like assemblyfileset etc, which means you cann't resuse reference assemblyfileset. For building multiple projects in a build that each project has an inter-set to others, each <csc> needs to have a full list of <reference><include> dlls. A bit of pain. Using <module> is a hack. Though it solves the resuse problem on the surface, if you poke the assembly with Reflector, you will find there are modules should really be references. this is NOT the right way to reference <assemblyfileset>.
<csc> ... <references refid="sys.assemblies " /> <modules>
<include name="${webcontrols.output.Mono}\${webcontrolsNamespace}.dll" />
<include name="${cenote.output.Mono}\${cenoteNamespace}.dll" />
</modules> </csc>

By the way, if declare <fileset>and its derived types like
<assemblyfileset> at project level(doc root) it can take an id attribue
and can be referenced from everywhere.
4. <nunit2> and nUnit2.2.x GUI
Leaving the bug in <csc> behind, I move into nant <target> unit
test with <nunit2>. nunit2 used nunit-version=" 2.2.8.0"
clr-version="2.0.50727.42" for the test. Passed. However, now the mono-target
assemble is not loaded with nUnit2.2.X GUI, not even nUnit-2.2.0-mono console
exe. An System.IO.FileNotFoundException is thrown, complaining test dll or its
dependencies dlls are not found. [to-do] I will look into it later. This is not
an issue for next step. (We only use nUnit gui for vsnet build)
5. Serve web test using ruby+watir
prerequisite: install Ruby
engine and Watir library
Watir offers a clean, well define, automated and scripting (in opposite to
recording) base Web app test.
Travis
made a nice hook to integrate ruby script into nUnit test
framework. It is not absolutely necessary to integrate ruby script into nUnit.
*.rb script can run from cmd line. However, the integration leverage the
reporting function comes with nUnit.
  1. compile RubyTestExecutor-1.1.3.0 to target nunit_2.2.0_mono
  2. Write a 'Hello World' test case as well as ruby script. In Nant task
    <csc>include them as embeded resource. <resources
    dynamicprefix="true"> <include name="...\*.rb" /> </resources>
  3. VSNET .net1.1 build and nUnit GUI test

    • in vsnet reference RubyTestExecutor-1.1.3.0_target_nunit_2.2.0
    • make sure the Web_Control_test_project is set to default web sharing. It
      contains test pages as well as test cases.
    • Postbuild action to copy test.dll and referenced dlls (except
      nUnit.framework.dll) from ~\bin\Debug to ~\bin
    • run tests

  4. VSNET mono1.0 build and nAnt build (test with <nunit2>, on my pc it picks
    the lastest nunit2.2.8 engine)

    • in nAnt build reference RubyTestExecutor-1.1.3.0_target_nunit_2.2.0_mono
    • compile, delpoy as usual
    • start XSP at Web_Control_test_project where pages live.
    • Both use port 80 for http request. This means we don't need to change ruby
      scripts for native 1.1 (which runs on IIS) or mono runtime (which runs XSP).
      Just need to stop/start IIS before XSP kicks start. (I argue it is more
      pragmatic then dynamic decide which port to use depends on runtime env)
5 Conclusion
With this build script, we archieve:
  • Implement, build and test ASP.Net Web Controls and Website in VSNET2003 IDE to
    target .Net 1.1 framework.
  • Use nAnt build script to link, compile, deploy the same code base to target
    Mono1.0. This build process needs to know nothing about the IDE build.
  • Use Ruby+Watir scripts to test web app in 'nearest' real way.The script test
    suite is seamlessly integrate into nUnit framework.
6 What's more can be done?
Automated XSP web server start/stop. The build process requires manually start
mono XSP web server before <nunit2> tests. XSP must run in an isolated
AppDomain/process from where nAnt is running from, so use <exec> task is
not prossible. It is also not possible to use
Application Domain
ExecuteAssembly
to create and load XSP into an
isolated AppDomain because on Windows OS XSP requires mono runtime environment,
which is unmanaged code. (Though XSP.exe is managed code.)

Download the build script template: myWebsite.build

Technorati Tags: nAnt

20 August 2006

TDD Mono asp.net web application on

Note: (This is a dump as I play with the new tools on doing familiar TDD aka. Test Driven Development), work in progress.

The application:
An asp.net web application run on Linux Mono

Requirement: Unit test web application UI.

– This is hard. NUNIT is designed for API (exe, lib) test

– Unit test server controls

– Support Continuous integration


Tools for the trade:

- VS.net IDE

- NUnit (For this project we targeting asp.net 1.1, so pick NUnit 2.2 from the list)

- Nant

- Ruby

- WATIR (I use watir-1.4.1.exe as at the time of writing)

- Mono gtk, XSP web server


Internal Resource:

- Ruby test code template for vs.net

- Water code template for vs.net (because I am lazy)


Reference:

Babysteps in WATIR A Jumpstart to Ruby/Watir

Integrated ASP.NET Web Application Testing with NUnit, Ruby, and Watir Travis Illig creates a RubyTestExecutor to integrate Ruby/Watir Testscripts into nUnit framework. So test cases can be run from GUI like other nUnit test cases. However this is more of convenience that necessary. You still write Ruby/Watir scripts. You can still invoke them from command line.

Introduction to Mono - Your first Mono app Happened to find this Primer


Development pattern:


Assumption: Coding in vs.net 2003, OS: winXP

(One off task) Create web project

  1. create project directory: {root}\My.Hello.World
  2. Web share this folder
  3. create a new web project in vs.net, map it to My.Hello.World
  4. Execute Mono run time test. Bring up command line console and do nAnt build.
    a. Start Mono XSP web server
    b. nAnt target doesn't natively require nUnit, although it could leverage point 1-3.
    c. However, this is redundant. We can just load up Ruby and Watir script engines and call test scripts
Technorati Tags: TDD RUBY WATIR NUNIT ASPNET

18 June 2006

TDD Moving from 1.1 to 2.0

Moving dev environement to VS 2005, which by default targets .Net framework 2.0. To make it targets 1.1 here is Jomo Fisher : Hack the Build: Use Whidbey Beta2 to target .NET Runtime 1.1, which works on VS 2005 full release as well.
I organise test assembly into a project within the same solution. It is quit cool to have differnt project configured to different .net framework versions.
So the class library(test target) is 2.0 while test fixture is 1.1, which statisfies nUnit console (2.2.0). Soon I run into chicken and egg problem - test asm reference test target - 1.1 on 2.0. Reverese dependency simply doesn't work.
Luckily folks at nUnit.org didn't stop at when MS recuit one of their top guy and build a MS Unit Test into their VS Team Sytem. (I tried it in a few ocassion - awful I say).
Quickly upgraded nUnit to 2.2.8 for .Net 2.0, put test asm back to 2.0. It works fine!

27 October 2005

Anti Intellisense with Test Driven Development

"I don’t think IntelliSense[note 1] is helping us become better programmers. The real objective is for us to become faster programmers, which also means that it’s cheapening our labor. " -- Charles Petzold A Talk Delivered at the NYC .NET Developer’s Group, October 20, 2005

I have to agree with Charles that Intellisense changes the way we code, it turns design and implementation solution to be kindof conversation with development environment and hence slave to it.

Luckily, here comes rescue: Test Driven Development. With it you can actually start your implementation with the requirement, demand and the burning need to fix the broken test case. You actually start with a question 'what do I want to achieve by doing this?' You then start with using a method, a member variable before had it defined. And with Re-Sharper, you can easily turn the undeclared member into a skeleton you can implement later.

Because they are undeclared when first used, Intellisense won't pop up.

Hurray!

07 April 2004

Why we need test first development

Here is an other classic example. I bet it will be used in some book sooner or later, like Test Driven Development. Here is the story:
...
"The Chinook helicopters could be a risk to fly in cloudy weather because the software which enables them to do this cannot be properly tested.
Fixing the problem will cost an estimated �£127m and the Chinooks will remain grounded until at least 2007. "Read more...