Unit testing workbooks and making them part of the development lifecycle

Hi,

You may find this useful, in short words, I needed to ensure that the workbooks were up to date, and even make them part of integration tests and acceptance tests. So I use the execution code as part of unit tests.

I cannot post links, so this the extract of a blog post:

Extracting and executing all the code from the workbooks

Workbooks mainly execute each of the code elements one by one using the Roslyn scripting engine. So if we extract all the code sections from the Markdown document, we can execute them inside of our unit test in the same way.

var code = GetCodeSectionsFromWorkbook();
var state = await CSharpScript.RunAsync(code);

The beauty of this, is that the code will be using the current project, library, nuget reference in our project, which will be the latest.
Executing the code can be fine, but how do we validate the results?

state = await state.ContinueWithAsync(“return (balanceSecondAmountSend, originalBalanceFirstAmoundSend);”);

I did not want to mix testing with the documentation, so a simple way to validate the data is to return all the variables that we want to Assert using the new multiple return capability in c#7.

Once we know the variable names, we can just add them to the script to be the final return statement.
Finally, we can cast the returned tuple as a dynamic type and assert each of the returned values in order.

var returnValue = (dynamic) state.ReturnValue;
Assert.Equal(2000, returnValue.Item1);
Assert.Equal(1000, returnValue.Item2);”

For a full code sample checkout in github.com "Nethereum/Nethereum.Workbooks" , or in medium the full blog post medium.com "@juanfranblanco/unit-or-integration-tests-of-xamarin-workbooks-6f206b8483d6"

J

Posts

Sign In or Register to comment.