foundry icon indicating copy to clipboard operation
foundry copied to clipboard

Code run in `setUp` functions is not counted in coverage.

Open Willyham opened this issue 2 years ago • 2 comments

Component

Forge

Have you ensured that all of these are up to date?

  • [X] Foundry
  • [X] Foundryup

What version of Foundry are you on?

forge 0.2.0 (a1f41be 2023-03-14T00:15:36.156534Z)

What command(s) is the bug in?

forge coverage

Operating System

macOS (Apple Silicon)

Describe the bug

In the below contract, the coverage report for the initialize function shows the assignment of foo = 1; to be uncovered, as that code is only executed in the setUp function.

pragma solidity ^0.8.13;

contract CoverageTest {
    uint256 public foo;
    function initialize() public {
      foo = 1;
    }

    function bar() public {
      foo = 2;
    }
}

and test file:

pragma solidity ^0.8.13;
import {Test} from "forge-std/Test.sol";

contract FooTest is Test {
  CoverageTest t;

  function setUp() public {
    t = new CoverageTest();
    t.initialize();
  }

  function testFoo() public {
    t.bar()
    assertEq(t.foo(), 2);
  }
}

It is not clear cut to me that the coverage data should include calls from setUp, but my real world example is that I have multiple branches in my initialize function, and I want to ensure that my test suite is covering all of those branches. I can write a specific test for that but it seems redundant when the tests which execute those branches already exist.

This may be related to or the same as #3453

Willyham avatar Mar 14 '23 09:03 Willyham

Same issue

arr00 avatar Apr 13 '23 01:04 arr00

After the support of constructor being added for coverage via this PR, I think it could be a good idea to consider the constructor code run in the setUp() to be included in coverage.

As in general people tend to deploy contracts in the setUp() method, and use it in multiple tests and in doing so the the code inside the constructor (which could be just assigning some immutables vars) comes out to be not covered in coverage.

brotherlymite avatar Apr 19 '24 06:04 brotherlymite

no longer the case with latest version, please reopen if you hit same behavior

image

grandizzy avatar Jun 27 '24 05:06 grandizzy

confirming this is fixed

gosuto-inzasheru avatar Jun 27 '24 17:06 gosuto-inzasheru