Yelp / Testify

A more pythonic testing framework.
Other
304 stars 67 forks source link

Add new function addfinalizer to TestCase #311

Closed ealter closed 9 years ago

ealter commented 9 years ago

Add functionality to test cases so that they can dynamically register teardowns. This is modeled after addfinalizer in pytest.

asottile commented 9 years ago

Let's call it addfinalizer

bukzor commented 9 years ago

What's the use case for this?

From what I saw in pytest, the addfinalizer was a more hackitty way to write a setup-teardown fixture. This is one of the very few spots where testify's design is better, and pytest is moving in this direction with their @yield_fixture's.

I'm quite happy to be wrong, but you'll need to explain it.

ealter commented 9 years ago

We want to turn factory classes into functions. When factories want to clean up after themselves, they need a way to do so. This new function will make it very simple to have factory functions dynamically add new teardowns. On Apr 27, 2015 5:37 PM, "Buck Evan" notifications@github.com wrote:

What's the use case for this?

From what I saw in pytest, the addfinalizer was a more hackitty way to write a setup-teardown fixture. This is one of the very few spots where testify's design is better, and pytest is moving in this direction with their @yeild_fixture's.

I'm quite happy to be wrong, but you'll need to explain it.

— Reply to this email directly or view it on GitHub https://github.com/Yelp/Testify/pull/311#issuecomment-96858722.

bukzor commented 9 years ago

How would this look in a user's test?

class MyTest(testify.TestCase):
    def mytest(self):
        fakeuser = factories.fakeuser(testcase=self)
        assert stuff

That sounds like a context manager to me, no? A factory defines a context in which a fake-x-object exists, and outside that context it does not exist.

I'd like to let tests look like this someday: (ie factor out testify.TestCase entirely, enable function-tests)

def mytest():
    with factories.fakeuser() as fakeuser:
        assert stuff

edit: s/fixture/context manager/

ealter commented 9 years ago

That is a much much harder refractor of yelp-main. Factories are used all over the place. What happens when you have tests that register factories in loops? Do we want to start having recursive tests? Tests that have nested all over the place with crazy indentation?

The entire point of this new function is to add syntastic sugar for this particular use case of teardowns.

--Eliot On Apr 27, 2015 5:47 PM, "Buck Evan" notifications@github.com wrote:

How would this look in a user's test?

class MyTest(testify.TestCase): def mytest(self): fakeuser = factories.fakeuser(testcase=self) assert stuff

That sounds like a fixture to me, no? A factory defines a context in which a fake-x-object exists, and outside that context it does not exist.

I'd like to let tests look like this someday: (ie factor out testify.TestCase entirely, enable function-tests)

def mytest(): with factories.fakeuser() as fakeuser: assert stuff

— Reply to this email directly or view it on GitHub https://github.com/Yelp/Testify/pull/311#issuecomment-96860464.

bukzor commented 9 years ago

@ealter There was totally a question there: "How would this look in a user's test?"

bukzor commented 9 years ago

fix'nship