Skip to content

Run Unit Tests Locally

Sunil Kumar edited this page Jun 29, 2023 · 8 revisions

Pre-requisites

Docker

You'll need docker, even if you'll run the tests without it. That's because tests in 0chain.net/smartcontract/dbs/event uses docker to dynamically run a temporary database container.

mockery

Mockery is a tool to generate mock files for go tests. Please refer to the documentation here for installation on different platforms.

rocksdb (recommended, only for no-docker runs)

Rocksdb is used in both miner and sharder, and it's a persistent key-value store. To install on macOS or Linux (steps for version 8.1.1):

wget https://github.com/facebook/rocksdb/archive/refs/tags/v8.1.1.tar.gz
tar -xvf v8.1.1.tar.gz
cd rocksdb-8.1.1
sudo make install-shared

Note: For macOS, especially Apple Silicon processors, libraries that you install using homebrew is save in /opt/homebrew/lib, which is not readable by ld compiler (the one used to compile go for build and test), so you'll need either to add this directory to ld path, or just add symlinks to the libraries you'll need to /usr/local/lib: ln -fs /opt/homebrew/lib/libzstd.dylib /usr/local/lib/libzstd.dylib for lztsd or libzstd as an example. You'll need to do so for the following libraries: libzstd, liblz4, libsnappy, libcrypto, libgmp libgmpxx. Please add to this wiki if you find any other libraries. If you find any trouble, feel free to communicate it in ochain-core channel.

Note: For Ubuntu Users, If you have all the dependencies installed on your machine, make sure they're all up-to-date. You can run sudo apt-get update

Refer to this page for more on rocksdb installation

mcl/bls

Those are cryptography libraries. To install for macOS/Linux:

git clone --recursive https://github.com/herumi/bls
cd bls
sudo make -C mcl -j 4 lib/libmclbn256.dylib install
sudo make MCL_DIR=./mcl MCL_USE_GMP=0 BLS_ETH=1 -j 4 install

Using Docker

We have a special docker compose project that runs unit testing, regardless of your device's OS or architecture, it builds an environment for the tests and runs them.

Note: You need Docker installed on your machine.

Bring the base docker container up for the unit tests:

cd 0chain
./docker.local/bin/build.base.sh

Run all the unit tests in the 0chain repo:

./docker.local/bin/unit_test.sh

Or, you can specify a specific module to run tests against

./docker.local/bin/unit_test.sh 0chain.net/smartcontract/dbs/event

It automatically generates mock files using mockery, this will take some time. If you need to disable this (needs to be run at least once), use --no-mocks flag.

./docker.local/bin/unit_test.sh --no-mocks

Without docker, using go test or just the IDE

  1. Generate mock files by running ./generate_mocks.sh at the root of 0chain repo.
  2. Run the go test command (run go help test if you need to learn more about it), but you need to add the tag bn256 for it, so will looks like go test -tags bn256 ...... You need to configure that in your IDE if you're going to use it for testing. In VSCode, you can configure it by adding this to .vscode/settings.json: "go.buildTags": "bn256"

Please don't hesitate to edit, comment or report any issue in 0chain-core channel.