Unit Testing
Unit testing with Zeus is quick and easy. Zeus also provides support for easily integrating DAPP Network services into your contracts.
Unit testing with Zeus is highly customizable and easy to configure. The following example explains how to use the main helper functions to write your own test.
You can also test with truffle npx truffle test.
To run all of the available unit tests in the ./tests directory run:
Test
Options
Truffle
1
zeus test
Copied!
1
zeus test [contract]
2
​
3
test
4
​
5
Options:
6
--version Show version number [boolean]
7
--storage-path path for persistent storage
8
[default: "/home/ubuntu/.zeus"]
9
--rc-file use rc file to load options from
10
[default: "/home/ubuntu/.zeus/zeusrc.json"]
11
--rc-ignore ignore rc file [default: false]
12
--verbose, -v [default: false]
13
-h, --help Show help [boolean]
14
--wallet, -w keosd wallet to use [default: "zeus"]
15
--creator-key private key to set contract to
16
[default: "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"]
17
--creator, -a eos account to set contract to [default: "eosio"]
18
--reset reset local environment [default: true]
19
--chain chain to work on [default: "eos"]
20
--network network to work on [default: "development"]
21
--compile, -c compile contract(s) [default: false]
22
--stake, -s account staking amount [default: "300"]
23
--transfer account staking amount [default: "1000"]
24
--basic-env, -b min needed for EOSIO smart contract test
25
[default: false]
26
--services service APIs to run [default: ""]
27
--delete-logs, -d delete logs folder before starting test
28
[default: false]
29
--multi-evm run multiple evm chains [default: false]
30
--enable-features enables eosio features [default: false]
31
--single-chain run without LiquidX [default: false]
32
--custom-token custom chain token [default: ""]
33
--custom-token-precision custom chain token permission [default: 4]
34
--evm-host custom evm host [default: ""]
35
--evm-port custom evm port [default: 4]
36
--evm-sister-host custom evm sister host [default: ""]
37
--evm-sister-port custom evm sister port [default: 4]
38
​
39
Examples:
40
zeus test contract
41
zeus test
42
zeus test contract -c
43
zeus test -c
44
zeus test --services "ipfs,cron,oracle,sign"
Copied!
1
npx truffle test
Copied!

Customize your own unit tests

in zeus_boxes/tests/mycontract.spec.js
1
require('mocha');
2
const { requireBox } = require('@liquidapps/box-utils');
3
const { assert } = require('chai'); // Using Assert style
4
const { requireBox } = require('@liquidapps/box-utils');
5
const { getCreateKeys } = requireBox('eos-keystore/helpers/key-utils');
6
const getDefaultArgs = requireBox('seed-zeus-support/getDefaultArgs');
7
const { getTestContract } = requireBox('seed-eos/tools/eos/utils');
8
const artifacts =requireBox('seed-eos/tools/eos/artifacts');
9
const deployer = requireBox('seed-eos/tools/eos/deployer');
10
const { genAllocateDAPPTokens, readVRAMData } = requireBox('dapp-services/tools/eos/dapp-services');
11
​
12
/*** UPDATE CONTRACT CODE ***/
13
var contractCode = 'mycontract';
14
var ctrt = artifacts.require(`./${contractCode}/`);
15
const { awaitTable, getTable, delay } = requireBox('seed-tests/lib/index');
16
​
17
describe(`${contractCode} Contract`, () => {
18
var testcontract;
19
​
20
/*** SET CONTRACT NAME(S) ***/
21
const code = 'airairairair';
22
const code2 = 'airairairai2';
23
var testUser = "tt11";
24
var account = code;
25
​
26
/*** CREATE TEST ACCOUNT NAME ***/
27
const getTestAccountName = (num) => {
28
var fivenum = num.toString(5).split('');
29
for (var i = 0; i < fivenum.length; i++) {
30
fivenum[i] = String.fromCharCode(fivenum[i].charCodeAt(0) + 1);
31
}
32
fivenum = fivenum.join('');
33
var s = '111111111111' + fivenum;
34
var prefix = 'test';
35
s = prefix + s.substr(s.length - (12 - prefix.length));
36
console.log(s);
37
return s;
38
};
39
​
40
before(done => {
41
(async () => {
42
try {
43
44
/*** DEPLOY CONTRACT ***/
45
var deployedContract = await deployer.deploy(ctrt, code);
46
47
/*** DEPLOY ADDITIONAL CONTRACTS ***/
48
var deployedContract2 = await deployer.deploy(ctrt, code2);
49
50
/*** ALLOCATE DAPP TOKENS TO YOUR DEPLOYED CONTRACT ***/
51
await genAllocateDAPPTokens(deployedContract, 'ipfs');
52
​
53
/*** RETURNS EOSJS SMART CONTRACT INSTANCE ***/
54
testcontract = await getTestContract(code);
55
​
56
/*** ENDS UNIT TEST SUCCESSFULLY ***/
57
done();
58
} catch (e) {
59
/*** FAILS UNIT TEST AND PROVIDES ERROR ***/
60
done(e);
61
}
62
})();
63
});
64
65
/*** DISPLAY NAME FOR TEST, REPLACE 'coldissue' WITH ANYTHING ***/
66
it('coldissue', done => {
67
(async () => {
68
try {
69
70
/*** SETUP VARIABLES ***/
71
var symbol = 'AIR';
72
73
/*** DEFAULT failed = false FOR ASSERTION ERROR ***/
74
/*** SET failed = true IN TRY/CATCH BLOCK TO FAIL TEST ***/
75
var failed = false;
76
77
/*** SETUP CHAIN OF ACTIONS ***/
78
await testcontract.create({
79
issuer: code2,
80
maximum_supply: `1000000000.0000 ${symbol}`
81
}, {
82
authorization: `${code}@active`,
83
broadcast: true,
84
sign: true
85
});
86
​
87
/*** CREATE ADDITIONAL KEYS AS NEEDED ***/
88
var key = await getCreateKeys(code2);
89
90
var testtoken = testcontract;
91
await testtoken.coldissue({
92
to: code2,
93
quantity: `1000.0000 ${symbol}`,
94
memo: ''
95
}, {
96
authorization: `${code2}@active`,
97
broadcast: true,
98
keyProvider: [key.active.privateKey],
99
sign: true
100
});
101
102
/*** ADD DELAY BETWEEN ACTIONS ***/
103
await delay(3000);
104
105
/*** EXAMPLE TRY/CATCH failed = true ***/
106
try {
107
await testtoken.transfer({
108
from: code2,
109
to: code,
110
quantity: `100.0000 ${symbol}`,
111
memo: ''
112
}, {
113
authorization: `${code2}@active`,
114
broadcast: true,
115
keyProvider: [key.active.privateKey],
116
sign: true
117
});
118
} catch (e) {
119
failed = true;
120
}
121
122
/*** ADD CUSTOM FAILURE MESSAGE ***/
123
assert(failed, 'should have failed before withdraw');
124
125
/*** ADDITIONAL ACTIONS ... ***/
126
​
127
done();
128
} catch (e) {
129
done(e);
130
}
131
})();
132
});
133
134
/*** USE it.skip TO CONTINUE WITH UNIT TEST IF TEST FAILS ***/
135
it.skip('it.skip does not assert and continues test if fails' ...
136
});
Copied!
Last modified 4mo ago