Posts Tagged ‘tdd’
RSpec — common base specs
Sometimes I am fond of creating inheritance chains for tests, which is easy in Java or C# say, but not quite as obvious in rspec.
Define and register a base class
It just needs to inherit from Spec::Example::ExampleGroup so it’s runnable.
require 'spec/example/example_group' class UnitTest < Spec::Example::ExampleGroup protected def you_can_call_this end end Spec::Example::ExampleGroupFactory.register(:unit_test, UnitTest)
Set the type when describing
describe 'By supplying the :type', :type => :unit_test do
it 'this example group inherits from whatever class you supply as :type' do
self.protected_methods.should include('you_can_call_this')
end
end
You now have common base class to your specifications.
Test driving and test coverage
Yesterday I enabled rcov on my current project and was surprised that it had 100 per cent unit test coverage.
But perhaps I should not have been surprised. After all, if you are truly test driving, how could you possibly leave anything uncovered?
Raking .NET projects in TeamCity
Faced with the unpleasant prospect of assembling yet another stack of xml files for an automated build, I thought I’d try rake instead. A couple of people here at 7digital have used Albacore before, so I started there.
1. Build
Use Albacore‘s msbuild task:
require 'albacore'
desc "Clean and build"
msbuild 'clean_and_build' do |msb|
msb.properties :configuration => :Release
msb.targets :Clean, :Build
msb.verbosity = "quiet"
msb.solution = "path/to/ProjectName.sln"
end
2. Run tests
This is also very straight forward with Albacore, but slightly more useful is applying the usual TeamCity test result formatting and reporting.
2.1 Tell your build where the NUnit test launcher is
TeamCity already has an NUnit runner, and the recommended way to reference it is with an environment variable.
Note: The runners are in the <TEAM CITY INSTALLATION DIR>/buildAgent/plugins/dotnetPlugin/bin directory.
2.2 Write the task
Once you have the path to the executable, you’re free to apply any of the available runner options.
Assuming you have added the TEAMCITY_NUNIT_LAUNCHER environment variable then the actual execution is then something like:
asm = 'ProjectName.Unit.Tests.dll'
nunit_launcher = ENV["TEAMCITY_NUNIT_LAUNCHER"]
sh("#{nunit_launcher} v2.0 x86 NUnit-2.5.0 #{asm}")
Beats hundreds of lines of xml I reckon.
References
- Albacore — rake tasks for.NET systems
- TeamCity NUnit launcher
- Setting environment variables for your TeamCity build
