Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code run in setUp functions is not counted in coverage. #4553

Closed
2 tasks done
Willyham opened this issue Mar 14, 2023 · 4 comments
Closed
2 tasks done

Code run in setUp functions is not counted in coverage. #4553

Willyham opened this issue Mar 14, 2023 · 4 comments
Labels
Cmd-forge-coverage Command: forge coverage T-bug Type: bug

Comments

@Willyham
Copy link

Component

Forge

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

  • Foundry
  • 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 Willyham added the T-bug Type: bug label Mar 14, 2023
@gakonst gakonst added this to Foundry Mar 14, 2023
@github-project-automation github-project-automation bot moved this to Todo in Foundry Mar 14, 2023
@arr00
Copy link

arr00 commented Apr 13, 2023

Same issue

@mds1 mds1 added the Cmd-forge-coverage Command: forge coverage label Apr 13, 2023
@brotherlymite
Copy link

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.

@grandizzy
Copy link
Collaborator

grandizzy commented Jun 27, 2024

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

image

@gosuto-inzasheru
Copy link

confirming this is fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Cmd-forge-coverage Command: forge coverage T-bug Type: bug
Projects
Archived in project
Development

No branches or pull requests

6 participants