title: Why is my pytest fixture with function scope returning an object that isn't resetting it's class variables in a new test?
body: <p>I have a class called Person().  It has a CURRENT_YEAR class variable, intended to be shared among all instances of the class. </p>
  
  <p>I was hoping that each of my tests in a single module would get a fresh (new) object since I scoped the fixture as 'function'.  However, when I change the CURRENT_YEAR in one test function, which happens using a class method that changes the Person.CURRENT_YEAR value, it persists into the next test function.  So clearly the object isn't getting wiped out and recreated for each test.</p>
  
  <p>The fixture is created in the conftest.py, accessible by all tests.</p>
  
  <p>In the end, I broke it all down, and moved things around, but keep seeing the same thing.  The Person() class is not getting instantiated more than once, as I would have expected.  How should a fixture be created, so that each test_ function gets its own scope for the Class? </p>
  
  <p>I've tried moving tests to separate modules, but it didn't help.</p>
  
  <p>I tried making a second fixture, that returns a Person() object. No difference. </p>
  
  <p>I've really stripped it down in the code below, so it's hopefully clear what I'm trying and why I'm confused.</p>
  
  <h1>project_root/tests/test_temp.py</h1>
  
  <pre><code>import os,sys
  tests = os.path.dirname(__file__)
  project = os.path.dirname(tests)
  sys.path.insert(0,project)
  import pytest
  from app.person import *
  
  def test_current_year_changes(person_fixture):
      import pytest
      p = person_fixture
      print(f"CY is {p.CURRENT_YEAR} and age is {p.get_age()}")
      p.add_years_to_age(20)
      print(f"CY is {p.CURRENT_YEAR} and age is {p.get_age()}")
      assert p.CURRENT_YEAR == 20
  
  def test_current_year_changes2(person_fixture2):
      import pytest
      p = person_fixture2
      print(f"CY is {p.CURRENT_YEAR} and age is {p.get_age()}")
      p.add_years_to_age(20)
      print(f"CY is {p.CURRENT_YEAR} and age is {p.get_age()}")
      assert p.CURRENT_YEAR == 20
  
  
  @pytest.fixture(scope='function')
  def person_fixture():
      p = Person()
      return p
  
  @pytest.fixture(scope='function')
  def person_fixture2():
      p = Person()
      return p
  </code></pre>
  
  <h1>project_root/app/person.py</h1>
  
  <pre><code>class Person(object):
  
      CURRENT_YEAR = 0
  
      def __init__(self, name=""):
          self.name = name
          self.birth_year = Person.CURRENT_YEAR
  
      def add_years_to_age(self, years=1):
          Person.CURRENT_YEAR += years
  
      def get_age(self):
          return Person.CURRENT_YEAR - self.birth_year
  </code></pre>
  
  <p>The code looks like both tests should be pretty independent.  But the second test function shows that the CURRENT_YEAR is not starting with a new class variable.</p>
  
  <p>The assert fails showing that the <code>Person.CURRENT_YEAR</code> is 40, instead of 20 </p>