Create and import helper functions in tests without creating packages in test directory using py.test

后端 未结 7 716
北恋
北恋 2020-12-24 04:57

Question

How can I import helper functions in test files without creating packages in the test directory?


Contex

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-24 05:38

    my option is to create an extra dir in tests dir and add it to pythonpath in the conftest so.

    tests/
        helpers/
          utils.py
          ...
        conftest.py
    setup.cfg
    

    in the conftest.py

    import sys
    import os
    sys.path.append(os.path.join(os.path.dirname(__file__), 'helpers'))
    

    in setup.cfg

    [pytest]
    norecursedirs=tests/helpers
    

    this module will be available with import utils, only be careful to name clashing.

提交回复
热议问题