Knowledge Base

Behavioural Driven Development

For testing small pieces of code like functions we use unit tests. For larger pieces of code you can use Behavioural Driven Development, BDD for short. Using this approach tests are written in a language that is understandable by both developers and other stakeholders.

A lot has been written about BDD. Like BDD

If you search on the web for Delphi tools for BDD using you get the following links:

  1. This (YouTube is Roman presenting those slides). These links are a presentation of his framework DelphiSpec. Very basic, but it shows how it’s done.
  2. Library: Delphi library for running automated tests written in plain language
  3. dSpec: dSpec (beta status) is a BDD framework for Delphi/Pascal built as an extension of DUnit.

 

DelphiSpec

Implementing feature tests using this framework is easy. Although DelphiSpec is not very well documented, it has a couple of demo projects that shows what is supported and what can be done. We implemented a Roman numeral converter. The zip file contains both the framework and the demo project.

Adding new tests is not difficult, but is all done manually. That is a shortcoming of this framework. Other than that: using this framework BDD can definitely be implemented for Delphi projects.

Demo_using_DelphiSpec

dSpec

At first dspec won’t compile. The reason for this is that it calls the constructor on Self from an instance method. This was a technique used before XE3 so that in a base-class one could write a function that could be called on objects that derive from that base-class to quickly create objects of that derived class.

This framework is used differently. Instead of using feature files, everything is done in code.

– First your class extends from TContext. This dSpec class extends from TTestCase (part of DUnit)

– This TContext has a property called Specify. This object has a bunch of overloaded That functions that give back an ISpecifier implementation that has a Should method.

– Using this framework you can write your statements like this:

Specify.That(FFloat).Should.Be.GreaterThan(1).And_.Be.LessThan(2);

In this case the FFloat was filled in a setup method. All other methods are part of the framework.

Roman numeral converter

Including the DelphiSpec framework into the project supports a limited set of the Gherkin language. It contains its own parser for this.

Support

The support for this is cumbersome in Delphi. The DelphiSpec framework that was written in 2015 seems to be the best we have and it doesn’t support parsing the feature files to pas files for further implementation. It’s all manual labour. Of course it is possible to implement a parser for this, but since work on the DelphiSpec framework seems to have stopped new features have to be added there as well to be fully up to date.

FeatureParser

Written by Johnny Nap
Delphi developer

Contact

Let us help you to realise your ambitions

GDK Software UK

(+44) 20 3355 4470

GDK Software USA

+1 (575) 733-5744