pytest-describe icon indicating copy to clipboard operation
pytest-describe copied to clipboard

Context Manager viability

Open caioaamaral opened this issue 1 year ago • 0 comments

To achieve a sintax similar to ruby's RSpec, it seems to me that the use of Context Managers could be an option:

with describe('a thing'):
    @pytest.fixture
    def foo():
        return 'foo'
    
    with it('does a thing', foo):
        assert foo == 'foo'

A potential issue is the 'assert' statements being evaluated before collection. We could prevent that by wrapping it inside a function that saves it for later evaluation:

with describe('a thing'):
    @pytest.fixture
    def foo():
        return 'foo'
    
    with it('does a thing', foo):
        the_foo = 'foos'
        _assert(foo == the_foo, f'the_foo should be foo, instead got {the_foo}')
    
    with it('does another thing', foo):
        _assert(foo == 'foo')

What would be the restrictions to implement such notation?

caioaamaral avatar Nov 24 '24 13:11 caioaamaral