title: How to run tests which are located in same file as production code with pytest?
body: <p>I know this violates any <a href="https://docs.pytest.org/en/latest/goodpractices.html" rel="nofollow noreferrer">best practices</a> which require/assume proper packaging of Python production code: In some cases it might be helpful to beeing able to define production and test code in the same file (e.g. in case of simple scripts). How can I run all or specific tests in a file with <code>pytest</code> then?</p>
  
  <p><strong>EDIT</strong> - The solution for my particular use case:</p>
  
  <p>file structure in <code>&lt;root&gt;</code>:</p>
  
  <pre><code>pytest.ini
  scrip_with_tests.py
  </code></pre>
  
  <p>content of <code>pytest.ini</code>:</p>
  
  <pre><code>[pytest]
  python_files = script_with_tests.py
  </code></pre>
  
  <p>content of <code>script_with_tests.py</code>:</p>
  
  <pre><code>import pytest  # this is not required, works without as well
  
  def test_always_pass():
      pass
  
  if __name__ == "__main__":
      main()
  </code></pre>
  
  <p><code>pytest</code> invocation in <code>&lt;root&gt;</code>:</p>
  
  <pre><code>pytest script_with_tests.py
  </code></pre>