This tutorial demonstrates how to write basic tests in Foundry for Solidity smart contracts, covering test setup using the setup() function to deploy fresh contracts before each test, writing passing tests with assertEqual to verify expected outcomes, creating failing tests to expect errors like underflow, and utilizing options like --vvv for detailed test output and --gas-report for gas consumption analysis.
Writing Basic Foundry Tests: Setup, Assertions, and Error Handling
Added:in this video I'll write a basic test in Foundry we'll first copy and paste our simple counter contract and then I'll explain about test setup we'll write a passing test and a failing test I'll also go over again how to test a specific file and again go over this option called Dash vvd and then that's the I'll run a gas report on the test in this video I'll be using this contractor writer test this is a simple counter contract and you can get this from solidity by example I'll copy this code and then inside my code editor I'll click on counter.so on their source and then paste the code save this file then next I'll open the test file counter.t.soul and since this is a default test that came when we initialized The Foundry project first I'm going to clear all of the code I'll clear this out and then also clear this and let's try compiling the contract inside my terminal I'll type Forge build okay the contact compiles so the next step we will write our test for the counter contract for each of the tests that we're going to execute we want to start with our brand new contract and to do that we'll need to deploy a new contract before we run each of the tests we can do this by declaring a function called setup function set up and this function must be either public or external so I'll make this public and inside here we can write code that will be executed before each of the test is executed for our example we will want to deploy a new contract so say counter is equal to new counter so if we have two tests before each test this function setup will be called it will deploy a new contract run the first test and before the second test is executed it will run the setup again deploy a new counter contract and then execute the new test let's write our first test we'll first write a passing test so I'll say function but before we write the test let's open account entire contract and see what we want to test so I'll click on the counter contract and then move it over to the right on the left we have our test contract and on the right we have our counter contract so we're going to be writing a passing test Let's test the ink function if we call the function Inc the state variable count should increment by one so I'll say test Inc and then name it public all functions must be either public or external and it must be prefixed with the name test here we're testing the increment function so we prefix it with test and then name our test Inc we'll say counter dot Inc this should increment the state variable count by one and we can check this by typing assert equal counter dot count when it's first initialized it is equal to zero we call the functioning so now it increments by one and now it should be equal to 1. okay let's run our first test so I'm going to open my terminal clear the dogs and then I'll type Forge test I only want to run the test for the counter contract if I open my file Navigator under the test folder there are two contracts counter.2 and contract that tests the hello world contact here I only want to execute the test for the counter contract so what I can do here is dash dash match path test counter.t.soul and then execute this command and you can see here that our first test testing passed let's double check this test if we change this test to 2 then the function should fail hit Ctrl s to save the test file open the terminal and then run the test again and you can see that the test failed so it looks like we are writing the correct test let's fix our test and then next let's write a failing test what I mean by a failing test is that we want to test for errors if we call some function inside the counter contract and if they're there it should be captured inside this test so say function test to test for failures we'll have to say test fail and what are we going to test let's test the function deck the state variable count is initialized to zero so if we call the deck before we call any of the functions count will be equal to zero zero minus one will cause an underflow so calling the deck function will turn on air we're testing this here test fail deck public and again after the test ink is executed the function setup will be called again so when this test fail deck is executed we'll have a brand new counter contract so this means that the count State variable will be equal to zero inside here count will be zero and when we call the function deck this will cause our underflow and here we want to test that call to deck will fail with our underflow and to do that you'll type counter dot deck open the terminal again clear the logs and then let's run the test again now we have two passing tests what happens if I change this to counter dot Inc incrementing the count from zero to one will not fail but here we're saying that we expect this function to fail but it will not so this means that this test must fail to show you this I'll run the test again and you can see here that test fail deck fail this is because Foundry expected this test to fail but it did not fail it succeeded I'll fix the test and next I'll show you another way to write a failing test when we call the function deck it resulted in on the full error and inside here we can be specific that we expect that the air to be underfall error to do that I'll say function I'll name this deck underflow public and inside here we're going to call counter dot deck again this function will throw an error with our underflow and stealth Foundry that we expect there to be on the floor we'll write BM dot expect rebirth and inside here we'll write there that we're expecting this will be STD there dot arithmetic error this code over here is testing the same thing as test fail deck except we're being more explicit about the type of error that we expect to receive we expect that this function call to deck fails with underfro so open the terminal clear those logs and then let's run the test again and now we have three passing tests okay for the last example let's test the deck function and to do that you'll first need to call increment several times so say function test deck public and then we'll first call the counter ink several times to increase the count counter the ink I'll call it twice and then we'll call that counter dot deck and what do we expect the count State variable to be at this point a third equal counter dot count we incremented twice and then decrement it once so we expect the count to be equal to one open the terminal again run the test and now we have four passing tests next I'm gonna go over the verbosity of the test remember that when we executed the test we had our option called Dash BBB what does this do and why is it useful well going back to our test let's make the test fail for example over here I'll change this to a 10. and then run the test again notice that it says that the test failed for a test deck but we don't know why it failed how can we get more details out of this test what we can do is put in the option Dash dbb and then we'll get more details on why the test failed if I scroll up I can see here why the test fail on the left side of the assert equals a garden one but on the right side we expect it to be 10 and if you want even more details what you can do is add another V and it will give you even more details this B option you can put up to five B's for details on what each of these be do I can type dash dash help and under the option for verbosity it lists how each of the bees will output details for the test I'll go back to the test and then fix it next I want to talk about the gas report option we'll execute the test again and this time we'll put in the option called dash dash gas report this will print out a gas report of the test execute the code again you can see here that four of our tests passed and under it we have a gas report for example the function to deck on average used 1758 gas and it called three times and the function to Ink on average used 15 044 gas the maximum was 22 344 and it called a function in three times so in this video we wrote the simple test for the counter contract I showed you how to write some setup code and how to write test step pass and how to write tests where we expect the function call to fail I talked about how to execute a specific test file how to Output details from the test and then gas report from the test
Up Next

RAG Explained: Embeddings, Sentence BERT, and HNSW Vector Databases
@umarjamilai
84.9K views•2023-11-27

Building Real-Time ML Pipelines with Feature Stores and MLOps Frameworks
@ODSCAI
5.1K views•2022-02-20

Bypassing Tor Censorship: Bridges and Pluggable Transport Guide
@Coding_ForEveryone
397 views•2024-06-11

Neural Networks Explained: Math, Layers, and Learning Fundamentals
@3blue1brown
21.9M views•2017-10-05
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Artificial Intelligence





































