Skip to content

Getting Started with Unit Tests

Matthew Little edited this page Oct 1, 2018 · 23 revisions

Install Prerequisites

Setup Project

Open command line / terminal and create a new C# project with the command dotnet new console --name YOUR_PROJECT_NAME

Example:

dotnet new console --name MyProject

Add the Meadow unit testing framework package with the command dotnet add package Meadow.UnitTestTemplate

cd MyProject
dotnet add package Meadow.UnitTestTemplate

Create a directory named contracts to place your Solidity source files (the directory must be named contracts).

mkdir contracts

Add your Solidity source files to the contracts directory.

Here's an example hello world Solidity contract that we'll save to our contracts directory as HelloWorld.sol. Solidity source files must have the .sol file extension.

pragma solidity ^0.4.24;

contract HelloWorld {

    event HelloEvent(string _message, address _sender);

    function renderHelloWorld () public returns (string) {
        emit HelloEvent("Hello world", msg.sender);
        return 'Hello world';
    }

}

Run dotnet build and you should see a GeneratedContracts directory with generated source files matching your Solidity contracts.

Create a .cs file in your project directory to add our contract test code. For example HelloWorldTests.cs.

Write a test class that 1) deploys our contract, 2) tests a function call result, and 3) tests a transaction execution and its event log.

using Meadow.JsonRpc.Types;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading.Tasks;

namespace MyProject
{
    [TestClass]
    public class HelloWorldTests : ContractTest
    {
        HelloWorld _contract;

        protected override async Task BeforeEach()
        {
            // Deploy our test contract
            _contract = await HelloWorld.New(RpcClient);
        }

        [TestMethod]
        public async Task ValidateCallResult()
        {
// TODO..
        }

        [TestMethod]
        public async Task ValidateTransactionEventResult()
        {
// TODO..
        }

Temp - command to add latest beta package dotnet add package Meadow.UnitTestTemplate -v 0.3.185-beta -s https://www.myget.org/F/hosho/api/v3/index.json

Clone this wiki locally