0

I'm using Pytest to run test on one file that goes like this:

def function():...

def test_function():...

def main():...

main()

I want Pytest to exclude the line that calls main, is it possible?

liorr
  • 764
  • 5
  • 21
XimaN
  • 1
  • 1
  • 7
    `if __name__ == "__main__": main()`. And no, `pytest` can not skip code execution. – hoefling Nov 16 '20 at 07:38
  • Yes, `pytest-cov` *can* exclude **considering** code as part of a coverage report. Disregarding comments and answers about **execution** of code, PyTest Coverage allows you to mark lines of code so they do not appear in the report: https://coverage.readthedocs.io/en/6.5.0/excluding.html See this answer which adds a little detail: https://stackoverflow.com/a/64628546/134044 – NeilG Jul 27 '23 at 02:16

1 Answers1

0

Pytest will execute only those functions with test as a prefix. Since you have already have a function def function() at the start there can be some issues running the entire test case. As @hoefling commented, pytest can not skip the code. Try adding the following at the end of you code:

if __name__=='__main__':
    main()
Libin Thomas
  • 829
  • 9
  • 18