0

I have code similar to https://stackoverflow.com/a/62289822/14303498 Now I added similar fixture "teardown_class". when I execute my code. Teardown_class is executed right after setup_class

@pytest.fixture(autouse=True, scope='class')
def setup_class(self, connect_db_fixture_from_conftest):
    print("\n ********* we are inside setup")
    sql = "INSERT INTO %s.%s ( %s ) VALUES ( %s )"
    DBHelper.execute_query(self,sql,connect_db_fixture_from_conftest)
   
def test_db(self, connect_db_fixture_from_conftest):
    print("**** inside test *********")
   
@pytest.fixture(autouse=True, scope='class')
def teardown_class(self, connect_db_fixture_from_conftest):
    print("\n ********* we are inside teardown")
    sql = "DELETE from table_name where key=value"
    DBHelper.execute_query(self,sql,connect_db_fixture_from_conftest)

Output from test is:

********* we are inside setup
********* we are inside teardown
**** inside test *********

I want:

********* we are inside setup
**** inside test *********
********* we are inside teardown

I tried this suggestion also but no luck https://stackoverflow.com/a/51089177/14303498

hoefling
  • 59,418
  • 12
  • 147
  • 194
anju
  • 25
  • 4
  • Add a `yield` as first line in `teardown_class`. See [Fixture finalization / executing teardown code](https://docs.pytest.org/en/stable/fixture.html#fixture-finalization-executing-teardown-code) for more details. – hoefling Nov 04 '20 at 07:24
  • Thank you very much. It workd with yield in teardown_class – anju Nov 04 '20 at 16:36
  • However I am seeing following error though in my code I see there is no direct calling of the fixture ```Fixture "teardown_class" called directly. Fixtures are not meant to be called directly, but are created automatically when test functions request them as parameters. See https://docs.pytest.org/en/stable/fixture.html for more information about fixtures, and https://docs.pytest.org/en/stable/deprecations.html#calling-fixtures-directly about how to update your code.``` – anju Nov 04 '20 at 17:30
  • Don't call fixtures directly (as regular functions). Only use them as test function arguments. – hoefling Nov 04 '20 at 19:37
  • @hoefling, Can you please be more specific? Hear is my teardown_class ```@pytest.fixture(autouse=True, scope='class') def teardown_class(self, connect_db_fixture_from_conftest): print("\n ********* we are inside teardown") sql = "DELETE from table_name where key=value" DBHelper.execute_query(self,sql,connect_db_fixture_from_conftest)``` – anju Nov 04 '20 at 20:30
  • Looks for places in code where you invoke the function, like `teardown_class()` etc. – hoefling Nov 04 '20 at 20:36
  • I did not have teardown_class anywhere else in the code. However changing teardown_class to teardown_module fixed the issue. Though I dont know the reason behind it. – anju Nov 04 '20 at 20:56
  • That's because naming a fixture `teardown_class` clashes with another plugin that runs nosetests. `pytest` has builtin support for running tests written with `nosetest` which reserves names `setup_class`, `teardown_class`, `setup_method`, `teardown_method`, `setup` and `teardown` for special functions that will be invoked automatically. You can disable this plugin explicitly: `pytest -p no:nose`, but it's easier to give the fixture a more meaningful and less abstract name. – hoefling Nov 05 '20 at 08:53

0 Answers0