ApeWorX / ape

The smart contract development tool for Pythonistas, Data Scientists, and Security Professionals
https://apeworx.io
Apache License 2.0
894 stars 131 forks source link

pytest scope fixtures do not work #1546

Open OwlOfMoistness opened 1 year ago

OwlOfMoistness commented 1 year ago

Environment information

$ ape --version
0.6.13

$ ape plugins list
Installed Plugins:
  solidity    0.6.7
$ cat ape-config.yaml
name: playgrnd

What went wrong?

When setting the scope of items in conftest, it seems like scoping does not work

Here is the conftest:

import pytest

@pytest.fixture(scope="session")
def minter(accounts):
    return accounts[0]

And here is the test that shows scoping doesnt not work

from ape import chain

def test_1(minter, chain):
    print()
    print(chain.blocks.height)
    chain.mine()
    print(chain.blocks.height)

def test_2(minter, chain):
    print()
    print(chain.blocks.height)
    chain.mine()
    print(chain.blocks.height)```

after runnin `ape test -s`
output:

platform linux -- Python 3.9.2, pytest-7.4.0, pluggy-1.2.0 -- /home/owl/.local/pipx/venvs/eth-ape/bin/python cachedir: .pytest_cache rootdir: /home/owl/apeworx_plygrd plugins: eth-ape-0.6.13, web3-6.6.1 collected 2 items

tests/test_test.py::test_1 0 1 PASSED tests/test_test.py::test_2 0 1 PASSED


expected outcome would have been 

tests/test_test.py::test_1 0 1 PASSED tests/test_test.py::test_2 1 2 PASSED```

How can it be fixed?

Unsure sadly, most likely in the ape_test directory, area that loads sends info to pytest?

antazoey commented 1 year ago

Hello!

I am guessing the fixture is indeed session-isolation (both via pytest and chain-isolation) but the tests themselves are still "function" scoped so they get the function isolation (including chain-restore) regardless.

I think what we could do is add a skip_isolation fixture or a @skip_isolation decorator to skip isolation for individual tests. Thoughts on that?

Note, we do have the --skip-isolation flag that will ignore all chain-isolation in your tests. Pytest-fixtures would still work normally but the chain would not do any snapshotting.