title: how to properly pytest a function with the same code used to seed a module scope fixture
body: <p>I have a fixture I created that pulls data from a database and puts it into a DataFrame so all subsequent tests can run on the same query output.  The same code is staged in a function that kicks of my data processing pipeline (which I am building all these tests around).  I am not sure I want to mock the db query result because I want to test the returned column names from the query and record uniqueness among other things.</p>
  <p>Where I am confused is how to properly test the function I have in my code that is doing the same thing as the <code>output_data</code> fixture.  Is it proper to duplicate the code in my app for the fixture?</p>
  <pre class="lang-py prettyprint-override"><code># df_service.py
  from project.settings import MSSQL_DB_CON_STR as con
  
  def df_from_sql(start_date, months):     
      sql = f&quot;SELECT * FROM dbo.awesome_table_value_function('{start_date}', {months})&quot;    
      return pd.read_sql(sql=sql, con=con)    
  </code></pre>
  <p>Here is the fixture I started with before I recognized I am creating a fixture for functionality I actually want to test and use as a fixture.</p>
  <pre class="lang-py prettyprint-override"><code># test_df_service.py
  
  import pytest
  import pandas as pd
  
  @pytest.fixture(scope=&quot;module&quot;)
  def output_data():
      from project.settings import MSSQL_DB_CON_STR as con
  
      start_date = &quot;11/1/2019&quot;
      months = 4
      sql = f&quot;SELECT * FROM dbo.awesome_table_value_function('{start_date}', {months})&quot;    
      return pd.read_sql(sql=sql, con=con)   
  
      return pd.read_sql(sql=sql, con=con)
  
  def test_columns(output_data):
      expected_columns = ['entity','attribute','value','effective_date']
      df = output_data
      for col in df.columns:
          assert col in expected_columns 
  
  </code></pre>