2. Patient Assertion - deftpack/testautomation GitHub Wiki

The idea behind the patient assertion is that, in certain use cases at the time of the condition check the resource is not ready, available yet and it is indeterministic when it will be. These situations usually are signs of bad design, and should be avoided but it is not always possible. All the network related operations bring that risk, and in this particular use case, none of the layers (not the browser nor selenium) emend this issue.

In order to get around this problem, patient asserter was created. The idea is very simple. An expression is passed to it, which then tries to evaluate it. When it fails either by throwing an exception or failing on the assertion, it will wait for a pre-described amount of time and try again. If the result do not change it will repeat this for a given time, otherwise just simply return immediately. When the maximum number of tries are reached it will just re-throw the exception.

Beside this nature it works pretty much as any other asserter, actually it is using NUnit's asserter beneath. Both the maximum number of try and the number of milliseconds to be wait between tries could be configured.

Configuration

The default number for maximum try is ten, and five hundred milliseconds for the wait.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="PatientAssertionConfiguration"
        type="DeftPack.TestAutomation.Assertion.PatientAssertionConfiguration, 
              DeftPack.TestAutomation.Assertion"/>
  </configSections>
  <PatientAssertionConfiguration numberOfTries="10"
                                 millisecondsBetweenTries="500">
  </PatientAssertionConfiguration>
</configuration>

Methods

There are only three methods to perform classic assertion.

It is very limited, but the reason is to rely on the fourth method that gives you pretty much limitless possibilities. Read more about the preferred constraint model in NUnit's documentation.