Guide: Unit Tests on Forked Mainnet (Lesson 10) #668
ZeroEkkusu
started this conversation in
Show and tell
Replies: 2 comments
-
|
This is an amazing contribution for the repository, thanks for this!! |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Awesome work here!! Love it :D |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Gm, fellow devs,
In Lesson 10, we have unit tests for our Python scripts.
If you tried making your own, though, some would fail because the state of the chain would change with each test.
Here's an example:
That's because
Test 1changed your WETH balance to 1 and you startedTest 2with that 1 WETH. In other words, the chain did not reset before runningTest 2.Here's how to do unit tests in the
mainnet-fork-devenvironment:conftest.pyfile in thetestsdirectorybrownie test --network mainnet-fork-devbrownie test --network kovanExplanation
@pytest.fixtureis something that will run prior to (and/or after) the test you pass it toscope="module"means that, if passed to a test, it will run once per file (module) - I keep my unit tests in separate files inside thetests/unitdirectory. Usescope="function"if you keep them all in a single filechain.reset()takes a snapshot before running the test and reverts to it when the test is done - ensuring the initial conditions are always the sameBtw, you can do the "Arrange" part of your test like this, too.
More info here.
HNY! 🥳
Beta Was this translation helpful? Give feedback.
All reactions