Skip to main content

Setting Up the Unit Test Environment

To streamline your OP_NET smart contract development and testing workflow, you can use a pre-configured template. This template simplifies the setup process, offering pre-configured scripts, commands, and examples tailored to OP_NET contracts.


Step 1: Clone the Template

Start with the official OP_NET Starter Template, which includes all necessary configurations, dependencies, and example contracts.

Clone the repository using the following commands:

git clone https://github.com/memoriesadrift/opnet-starter-template
cd opnet-starter-template

Step 2: Install Dependencies

After cloning the repository, install the required dependencies:

npm install
warning

Ensure that you have all the prerequisites installed on your system. For more information, refer to the Prerequisites guide.


Step 3: Using the Template

Compile Contracts

To compile the contracts provided in the template:

make compile

Run Unit Tests

To execute all unit tests:

make test

To run a specific test file:

make test-only name=<testFileName>

Example:

make test-only name=vesting

Clean the Project

To remove build artifacts and reset the environment:

make clean

Project Structure

This template follows a well-organized structure for managing contracts, tests, and compiled bytecode:

opnet-starter-template/
├── src/
│ ├── vesting/ # Example contract folder
├── __test__/ # Test files
│ ├── unit/
│ │ ├── contracts/ # Contract interfaces
│ │ ├── tests/ # Unit test files
├── build/ # Compiled contract bytecode
├── lib/
│ ├── bytecode/ # Bytecode of external dependencies
├── Makefile # Build and testing automation
├── package.json # Project metadata and dependencies

Heuristics for Automation

Contract Heuristics

  • Contracts are defined in the src/ directory, with a Blockchain.contract reference in the index.ts file.
  • Each contract is compiled into a .wasm file using the directory's name. For example, src/vesting/index.ts compiles to build/vesting.wasm.

Test Heuristics

  • Unit test files are located in the __test__/unit/tests/ directory.
  • For running specific tests, provide the file name without the extension. For example:
    make test-only name=vesting

Integration Testing

Integration testing is included with basic examples to interact with deployed contracts. Set up environment variables according to the .env.template file provided in the repository.

Run integration tests using:

make interact

Next Steps

Once your environment is set up, you can begin writing and running your unit tests. For details on how to write test files, refer to the Writing Unit Tests guide.