helpers icon indicating copy to clipboard operation
helpers copied to clipboard

Investigate the need for `@pytest.fixture(autouse=True)`

Open heanhsok opened this issue 1 year ago • 7 comments

Follow up from https://github.com/causify-ai/helpers/pull/152#discussion_r1896769789

    @pytest.fixture(autouse=True)
    def setup_teardown_test(self):

Is this needed? Originally this was a hack for some glitches in pytest. Can you file an issue and investigate why / if it's still needed?

FYI @gpsaggese @jsmerix

heanhsok avatar Dec 24 '24 23:12 heanhsok

I am not sure what is meant by the word "hack" here. This is a normal mechanism to run stuff before and after a test. There exist multiple such mechanisms to do set-up and tear down work, this is one of the options. For example unittest lib also has some built in options.

jsmerix avatar Dec 30 '24 10:12 jsmerix

@heanhsok let's try to assign issues to GH Projects. E.g., sometimes we can use dev_system-v2.x as placeholder

gpsaggese avatar Jan 02 '25 18:01 gpsaggese

@sonniki has some mental state. Pls add the link to the doc of why is needed

gpsaggese avatar Jan 22 '25 14:01 gpsaggese

Here is the doc detailing our current approach and why it was chosen: https://github.com/causify-ai/helpers/blob/master/docs/coding/all.write_unit_tests.how_to_guide.md#use-set_up_test--tear_down_test

Here is the old issue where the problem with the native setUp/tearDown methods was discussed and this fixture solution was proposed and implemented: https://github.com/causify-ai/csfy/issues/3466

sonniki avatar Jan 22 '25 16:01 sonniki

Through some readings, here’s my understanding:

  • Fixture: A fixture is a function that sets up some context or state that tests can use, like initializing resources or creating objects.
  • Autouse: When set to True, the fixture will be automatically used for all tests, even if the test functions do not explicitly request the fixture.
  • The @pytest.fixture(autouse=True) decorator is used to define a fixture that will automatically be applied to all tests in a test class without explicitly needing to be passed as an argument to the test functions.
  • Fixture is a recommended approach for setting up Teardown/Cleanup, which is what we are also doing for our setup_teardown_test method.
  • Therefore, this approach remains necessary.

In this case, should we close this for now? @sonniki

heanhsok avatar Jan 28 '25 18:01 heanhsok

As far as I am concerned, this approach is indeed completely fine. However, I think it was @gpsaggese who is initially skeptical, not me, so maybe it should be up to him whether we should discuss further or close.

sonniki avatar Jan 28 '25 19:01 sonniki

At this point I would just add some of the comments in the documentation https://github.com/causify-ai/helpers/issues/177#issuecomment-2607640349 https://github.com/causify-ai/helpers/issues/177#issuecomment-2619716409 and then close

gpsaggese avatar Apr 30 '25 12:04 gpsaggese