This project is read-only.

Plain text scenarios

Sep 24, 2009 at 11:37 PM

I'm keen to start applying NBehave plain text scenarios on my project, but I have a few questions I'm hoping someone can help me with:

  1. How does NBehave match up the [ActionSteps] class against the .scenario file?  I expect I'd want a number of different [ActionSteps] classes.
  2. The example GameSpec.cs class stores some state in the [ActionSteps] class.  Does NBehave create an instance of this class per scenario?
  3. Is there any equivalent to the classic TDD [Setup] and [Teardown] methods?



Sep 28, 2009 at 11:15 AM

1. If you use the 0.4 release the first action that matches is used, if you go by trunk you can implement the interface IMatchFiles in your [ActionSteps] classes to get control over which scenario files "belongs" to which class(es).

2. NBehave creates the instance once so that instance is reused between scenarios

3. There are no [SetUp] or [Teardown] like attributes, I have considered adding that. Feel free to send me a patch for it :)


Sep 28, 2009 at 8:28 PM

The IMatchFile interface was exactly what I was after.  Thanks!

I've opened issue 47 on the Google code site for Setup / TearDown style attributes in plain text scenarios, and I've uploaded a patch.  Please can you review and commit it.


Sep 28, 2009 at 9:56 PM

patch committed.

Mar 26, 2010 at 3:38 PM

Hi John,

Could you please post a bit of code to show how to use the IFileMatcher?

Thank you very much.


Apr 6, 2010 at 2:32 AM


Your [ActionSteps] class needs to implement IMatchFiles, which defines a read-only FileMatcher property (code for the interface is viewable  Your implementation of IFileMatcher can then match using whatever algorithm you think best.  Here's one I prepared earlier which assumes that .scenario files are grouped into directories that correspond to namespaces in code (although only a single level of nesting is used):


    public class ProformaSteps : ActionStepsBase, IMatchFiles



public class ProformaSteps : IMatchFiles
        public IFileMatcher FileMatcher
            get { return new MatchDirectoryToNamespace(GetType()); }

        // Snip

public class MatchDirectoryToNamespace : IFileMatcher
    private readonly string _nameToMatch;

    public MatchDirectoryToNamespace(Type actionStepClass)
        _nameToMatch = actionStepClass.Namespace

    public bool IsMatch(string fileName)
        var info = new DirectoryInfo(fileName);
        return info.Parent.Name == _nameToMatch;