title: Execute some code before Pytest runs
body: <p>I'm using Pytest to test my code and I'm running into a little, but an infuriating issue.</p>
  
  <p>One of the first things my program does is checking if there are any setting files available. If there aren't any it throws an error and calls <code>exit()</code>. This works well during normal runtime but messes with Pytest.</p>
  
  <p>The solution I came up with is to simply create a temporary settings file for the duration of the tests, by copying the template settings file. I've already written and successfully tested the code to achieve that.</p>
  
  <p>The problem I'm running into is that I can't find a Pytest hook that truly fires before everything else. This results in the program throwing the error before Pytest tries to create a temporary settings file. Which results in Pytest failing before it can perform any tests.</p>
  
  <p>Does anyone know of a way to fire a function before Pytest does or loads anything? Preferably within Pytest itself.</p>
  
  <h2>Some code context:</h2>
  
  <h3>Throwing the error and exit</h3>
  
  <p>This snippet runs on the import of the <code>settings</code> module.</p>
  
  <pre><code>if len(cycles) == 0:
      log.error("No setting files found. Please create a file " +
                "in the `settings` folder using the Template.py.")
      exit()
  </code></pre>
  
  <h3>Creating the temporary settings file</h3>
  
  <p>This code should be the very first thing Pytest runs.</p>
  
  <pre><code>def pytest_sessionstart(session):
      """ Create a test settings file """
      folderPath = os.path.dirname(os.path.abspath(__file__))
      folderPath = os.path.split(folderPath)[0] + "/settings"
  
      srcfile = folderPath + '/Template.py'
      dstfile = folderPath + '/test.py'
  
      shutil.copy(srcfile, dstfile)
  </code></pre>
  
  <h3>Removing the temporary settings file</h3>
  
  <p>This code should be one of the last things Pytest runs.</p>
  
  <pre><code>def pytest_sessionfinish(session, exitstatus):
      """ Delete the test settings file """
      folderPath = os.path.dirname(os.path.abspath(__file__))
      folderPath = os.path.split(folderPath)[0] + "/settings"
  
      os.remove(folderPath + "/test.py")
  </code></pre>
  
  <h3>Pytest output</h3>
  
  <p>With the call to <code>exit()</code> disabled, so you can see the execution order.</p>
  
  <pre class="lang-none prettyprint-override"><code>Lakitna$ pytest -v -s
   No setting files found. Please create a file in the `settings` folder using the Template.py. 
  TEMPORARY SETTINGS FILE CREATED
  ========================= test session starts ==========================
  platform darwin -- Python 3.6.4, pytest-3.3.1, py-1.5.2, pluggy-0.6.0 -- /usr/local/opt/python3/bin/python3.6
  cachedir: .cache
  rootdir: /Users/lakitna/Documents/Github/Onaeri-tradfri/Onaeri, inifile:
  collected 24 items
  </code></pre>