4

In a foundry project, I have a few fuzzing tests that are failing but do not want to remove them from my file.

For example, in mocha/chai you have a .skip keyword that will let you skip some tests. Is there anyway to skip specific function to run in Foundry?

5 Answers 5

7

In addition to the @Olivier Demeaux answer. If I needed to skip some tests I would do

  1. Make tests internal/private. Forge will run only public or external tests
  2. Remove the "test" word to skip specific tests as forge runs only functions that start with the test
  3. Move skipping tests to other contract and run command like forge test --no-match-contract "SkippedTestsContract"
  4. Write the word "Skip" or any other helper word at the end of specific tests and run command like forge test --no-match-test "Skip" Basically, you can put any helper word somewhere in the middle but it will make it harder to write the pattern in this place --no-match-test "pattern"
4
  • Cool, I didn't know about #4! Thanks! Commented Dec 22, 2022 at 13:03
  • I may have misled you, so I will clarify this point. What I meant by option 4 is that you can write any helper word to clarify which test you want to skip, and then by writing forge test --no-match-test "Skip", you are basically writing a pattern to be excluded from running the tests. In principle, you could write the word "Skip" or any other word somewhere in the middle, but this will make it harder to write the pattern.
    – AGJoYy
    Commented Dec 22, 2022 at 13:15
  • As long as there is 'skip' somewhere in the test name, that test will be ignored by Forge at test time. Correct? Commented Dec 22, 2022 at 13:17
  • 1
    Basically yes, but it's not specifically defined by the "skip" word. You can also write the test named testAPlusB and then run --no-match-test "Plus" and this test won't be tested. In general, option 4 is the same as your answer, the only thing I suggested is not to use the full name of the test but to use the regex pattern.
    – AGJoYy
    Commented Dec 22, 2022 at 13:30
4

Yes you can!

You just have to add "no_match_test" to your foundry.toml file, then the name of the test.

I ran my tests, then added the line

no_match_test = "testClaimTreasure"

to my foundry.toml file, then ran the tests again.

This is the resultenter image description here

As you can see, the test "testClaimTreasure" was not executed.

Hope this helps!

4

For any forge test, we support filtering via matches.

➜  ~ forge t --help
Run the project's tests.

Usage: forge test [OPTIONS]

Options:
  -h, --help
          Print help information (use `-h` for a summary)

Test filtering:
  -m, --match <PATTERN>
          Only run test functions matching the specified regex pattern.

          Deprecated: See --match-test

      --match-test <REGEX>
          Only run test functions matching the specified regex pattern

          [aliases: mt]

      --no-match-test <REGEX>
          Only run test functions that do not match the specified regex pattern

          [aliases: nmt]

      --match-contract <REGEX>
          Only run tests in contracts matching the specified regex pattern

          [aliases: mc]

      --no-match-contract <REGEX>
          Only run tests in contracts that do not match the specified regex pattern

          [aliases: nmc]

      --match-path <GLOB>
          Only run tests in source files matching the specified glob pattern

          [aliases: mp]

      --no-match-path <GLOB>
          Only run tests in source files that do not match the specified glob pattern

          [aliases: nmp]

      --debug <TEST_FUNCTION>
          Run a test in the debugger.

          The argument passed to this flag is the name of the test function you want to run, and it works the same as --match-test.

          If more than one test matches your specified criteria, you must add additional filters until only one test is found (see --match-contract and --match-path).

          The matching test will be opened in the debugger regardless of the outcome of the test.

          If the matching test is a fuzz test, then it will open the debugger on the first failure case. If the fuzz test does not fail, it will open the debugger on the last fuzz case.

          For more fine-grained control of which fuzz case is run, see forge run.

      --gas-report
          Print a gas report

          [env: FORGE_GAS_REPORT=]

      --allow-failure
          Exit with code 0 even if a test fails

          [env: FORGE_ALLOW_FAILURE=]

You can use the --no-match-test <regex>, --no-match-contract <regex>, --no-match-path <regex> options as shown in the help menu to skip running certain tests.

For example, if you had a bunch of tests called: testWethWorks, testWethDoesNotWork1 and testWethDoesNotWork2 in a TestWeth contract under test/TestWeth.t.sol you could do the following:

  • forge t --no-match-test testWethDoesNot*
  • forge t --no-match-contract TestWeth
  • forge t --no-match-path ./test/TestWeth.t.sol

Hope this helps.

1
  • Do you know how to ignore multiple patterns with the --no-match-contract flag? Commented Sep 15, 2023 at 10:44
0

Reverse engineered answer:

To match test with only the function you want to test

forge test --match-test "NameOfFunc" 
0

If you want to skip tests programmatically, add a custom function at the top of your test that returns nothing when a condition is met. This would act like a skip.

Something like:

enum Network {
    ARBITRUM,
    ETHEREUM,
    ETH_N_MOCKS
}

Network constant n = Network.ETHEREUM;
function _skip() internal pure returns(bool) {
   if (n == Network.ETHEREUM) return true;
   return false;
}

function test_something() public {
   if (_skip()) return;

   //your test code...

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.