9.区块链系列之hardhat框架测试合约
先前我们讲解了如何部署智能合约,今天我们来对合约进行测试,这是非常重要的一部分,毕竟一旦部署后不可变,如果测试不充分,那么黑客就不会客气了
1. 单元测试
在test目录下新建test-deploy.js
const { ethers } = require("hardhat")
const { expect, assert } = require("chai")
describe("SimpleStorage", function () {
let simpleStorageFactory, simpleStorage
beforeEach(async function () {
simpleStorageFactory = await ethers.getContractFactory("SimpleStorage")
simpleStorage = await simpleStorageFactory.deploy()
})
it("Should start with a favorite number of 0", async function () {
const currentValue = await simpleStorage.retrieve()
const expectedValue = "0"
assert.equal(currentValue.toString(), expectedValue)
})
it("Should update when we call store", async function () {
const expectedValue = "7"
const transactionResponse = await simpleStorage.store(expectedValue)
await transactionResponse.wait(1)
const currentValue = await simpleStorage.retrieve()
assert.equal(currentValue.toString(), expectedValue)
})
it("Should work correctly with the people struct and array", async function () {
const expectedPersonName = "shenjian"
const expectedFavoriteNumber = "6"
const transactionResponse = await simpleStorage.addPerson(
expectedPersonName,
expectedFavoriteNumber
)
await transactionResponse.wait(1)
const { favoriteNumber, name } = await simpleStorage.people(0)
assert.equal(name, expectedPersonName)
assert.equal(favoriteNumber, expectedFavoriteNumber)
})
})
运行yarn hardhat test
(base) PS D:\blockchain\blockchain\hardhat-simple-storage-fcc> yarn hardhat test
yarn run v1.22.19
$ D:\blockchain\blockchain\hardhat-simple-storage-fcc\node_modules\.bin\hardhat test
SimpleStorage
✔ Should start with a favorite number of 0
✔ Should update when we call store
✔ Should work correctly with the people struct and array (46ms)
3 passing (3s)
Done in 4.42s.
2. GAS报告
-
注册https://pro.coinmarketcap.com/以获得API_KEY
-
在.env文件中添加配置
COINMARKETCAP_API_KEY=刚才注册的API_KEY
- 在hardhat中新增以下代码
const COINMARKETCAP_API_KEY = process.env.COINMARKETCAP_API_KEY
module.exports = {
......
gasReporter: {
enabled: true,
currency: "USD",
outputFile: "gas-report.txt",
noColors: true,
coinmarketcap: COINMARKETCAP_API_KEY
},
}
- 运行
yarn hardhat test
生成gas-report.txt
·-------------------------------|----------------------------|-------------|-----------------------------·
| Solc version: 0.8.8 · Optimizer enabled: false · Runs: 200 · Block limit: 30000000 gas │
································|····························|·············|······························
| Methods │
··················|·············|··············|·············|·············|···············|··············
| Contract · Method · Min · Max · Avg · # calls · usd (avg) │
··················|·············|··············|·············|·············|···············|··············
| SimpleStorage · addPerson · - · - · 112419 · 2 · - │
··················|·············|··············|·············|·············|···············|··············
| SimpleStorage · store · - · - · 43724 · 2 · - │
··················|·············|··············|·············|·············|···············|··············
| Deployments · · % of limit · │
································|··············|·············|·············|···············|··············
| SimpleStorage · - · - · 463670 · 1.5 % · - │
·-------------------------------|--------------|-------------|-------------|---------------|-------------·
3. 测试代码覆盖率查看
运行yarn hardhat coverage
命令
--------------------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
--------------------|----------|----------|----------|----------|----------------|
contracts\ | 100 | 100 | 100 | 100 | |
SimpleStorage.sol | 100 | 100 | 100 | 100 | |
--------------------|----------|----------|----------|----------|----------------|
All files | 100 | 100 | 100 | 100 | |
--------------------|----------|----------|----------|----------|----------------|
所有代码已上传至https://gitee.com/SJshenjian/blockchain/tree/master/hardhat-simple-storage-fcc