Controlling test flow in FitNesse (making it stop running rows)

with terms

This article applies directly to FitNesse .NET’s DoFixture, but can be adapted for use in other fixtures. One of FitNesse’s greatest weaknesses is the lack of flow control within tests. In everyday real world automated testing there are plenty of times where you will want a test to stop executing so that it does not waste time or cause damage. First create some variables to use to control test flow. 
 

private Boolean stopExecutingRows = false;
private Boolean stopOnFailure = false;
private Boolean stopOnException = false;

Next you will need to override the MethodCells method in your fixture.
 

protected override System.Collections.IEnumerable MethodCells(CellRange theCells) {
  if ((stopExecutingRows == true)&&(theCells.Cells.Body.ToUpper() == "RESUME")) {
    stopExecutingRows = false;
  }
  if (stopExecutingRows == true) {
    throw new Exception("THIS ROW IS BEING SKIPPED");
  }
  else {
    return base.MethodCells(theCells);
  }
}

After that, you need to override the Wrong and Exception methods so that you can know when errors or exceptions happen. 

 

public override void Exception(Parse cell, Exception Exception) {
  if (stopOnException == true) {
    stopExecutingRows = true;
  }
  base.Exception(cell, Exception);
}
public override void Wrong(Parse cell, string actual) {
  if (stopOnFailure == true) {
    stopExecutingRows = true;
  }
  base.Wrong(cell, actual);
}
public override void Wrong(Parse cell) {
  if (stopOnFailure == true) {
    stopExecutingRows = true;
  }
  base.Wrong(cell);
}

Finally, create some methods that can be used in the tests.
 

public void SkipSubsequentRowsIfAStepFails() {
  stopOnFailure = true;
}
public void SkipSubsequentRowsIfAStepThrowsAnException() {
  stopOnException = true;
}
public bool Resume(){
  return true;
}

Then your FitNesse test can have rows like this:
 

|Skip Subsequent Rows If A Step Fails|
|Do Something|Funny|
|Check|Is|Everyone|Laughing|true|
|Do Something|Hilarious|
|Check|Is|Everyone|Laughing|true|
|Do Something|Mean|
|Check|Is|Everyone|Laughing|true|
|This row would be skipped|
|So would this one|
|Resume|
|This row will be executed now|
|And so on...| 

 

AttachmentSizeHitsLast download
DoFlowDemo.cs4.24 KB795 days 43 min ago
DoFlowDemo.fit893 bytes746 hours 35 min ago