New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running `py.test -h` causes exception #289

Closed
willharris opened this Issue Oct 22, 2015 · 8 comments

Comments

Projects
None yet
3 participants
@willharris

willharris commented Oct 22, 2015

Running py.test -h causes the following exception:

(env)[17:22:39] 馃幐 ~/Workspaces/wkspc % py.test -h                                                                                                                                   [鈼忊棌][testing-changes] 
Traceback (most recent call last):
  File "/Users/harris/Workspaces/env/bin/py.test", line 11, in <module>
    sys.exit(main())
  File "/Users/harris/Workspaces/env/lib/python2.7/site-packages/_pytest/config.py", line 48, in main
    return config.hook.pytest_cmdline_main(config=config)
  File "/Users/harris/Workspaces/env/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 724, in __call__
    return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
  File "/Users/harris/Workspaces/env/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 338, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/Users/harris/Workspaces/env/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 333, in <lambda>
    _MultiCall(methods, kwargs, hook.spec_opts).execute()
  File "/Users/harris/Workspaces/env/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 596, in execute
    res = hook_impl.function(*args)
  File "/Users/harris/Workspaces/env/lib/python2.7/site-packages/_pytest/helpconfig.py", line 59, in pytest_cmdline_main
    config._do_configure()
  File "/Users/harris/Workspaces/env/lib/python2.7/site-packages/_pytest/config.py", line 830, in _do_configure
    self.hook.pytest_configure.call_historic(kwargs=dict(config=self))
  File "/Users/harris/Workspaces/env/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 729, in call_historic
    self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
  File "/Users/harris/Workspaces/env/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 338, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/Users/harris/Workspaces/env/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 333, in <lambda>
    _MultiCall(methods, kwargs, hook.spec_opts).execute()
  File "/Users/harris/Workspaces/env/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 596, in execute
    res = hook_impl.function(*args)
  File "/Users/harris/Workspaces/env/lib/python2.7/site-packages/pytest_django/plugin.py", line 250, in pytest_configure
    _setup_django()
  File "/Users/harris/Workspaces/env/lib/python2.7/site-packages/pytest_django/plugin.py", line 137, in _setup_django
    from django.db.models import get_models
  File "/Users/harris/Workspaces/env/lib/python2.7/site-packages/django/db/models/__init__.py", line 5, in <module>
    from django.db.models.query import Q
  File "/Users/harris/Workspaces/env/lib/python2.7/site-packages/django/db/models/query.py", line 17, in <module>
    from django.db.models.deletion import Collector
  File "/Users/harris/Workspaces/env/lib/python2.7/site-packages/django/db/models/deletion.py", line 4, in <module>
    from django.db.models import signals, sql
  File "/Users/harris/Workspaces/env/lib/python2.7/site-packages/django/db/models/sql/__init__.py", line 4, in <module>
    from django.db.models.sql.subqueries import *
  File "/Users/harris/Workspaces/env/lib/python2.7/site-packages/django/db/models/sql/subqueries.py", line 12, in <module>
    from django.db.models.sql.query import Query
  File "/Users/harris/Workspaces/env/lib/python2.7/site-packages/django/db/models/sql/query.py", line 22, in <module>
    from django.db.models.sql import aggregates as base_aggregates_module
  File "/Users/harris/Workspaces/env/lib/python2.7/site-packages/django/db/models/sql/aggregates.py", line 9, in <module>
    ordinal_aggregate_field = IntegerField()
  File "/Users/harris/Workspaces/env/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 116, in __init__
    self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
  File "/Users/harris/Workspaces/env/lib/python2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
    self._setup(name)
  File "/Users/harris/Workspaces/env/lib/python2.7/site-packages/django/conf/__init__.py", line 47, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Running py.test on its own executes Django tests as expected.

OS X 10.11.1
Python 2.7.10 from Homebrew
pytest==2.8.1
pytest-django==2.9.1

@rnovacek

This comment has been minimized.

rnovacek commented Nov 11, 2015

Any progress here? I'm also seeing this exception when I run "py.test --help".

@pelme

This comment has been minimized.

Member

pelme commented Dec 5, 2015

I cannot reproduce this, can you give some more details?

~ 鈫 vf tmp
New python executable in /Users/andreas/.virtualenvs/tempenv-73e031286076a/bin/python
Installing setuptools, pip...done.
鈥箃empenv-73e031286076a鈥 ~ 鈫 pip install pytest-django==2.9.1 pytest==2.8.1
Downloading/unpacking pytest-django==2.9.1
  Downloading pytest_django-2.9.1-py2.py3-none-any.whl
Downloading/unpacking pytest==2.8.1
  Downloading pytest-2.8.1-py2.py3-none-any.whl (148kB): 148kB downloaded
Downloading/unpacking py>=1.4.29 (from pytest==2.8.1)
  Downloading py-1.4.31-py2.py3-none-any.whl (81kB): 81kB downloaded
Installing collected packages: pytest-django, pytest, py
Successfully installed pytest-django pytest py
Cleaning up...
鈥箃empenv-73e031286076a鈥 ~ 鈫 py.test -h
usage: py.test [options] [file_or_dir] [file_or_dir] [...]

positional arguments:
  file_or_dir

general:
  -k EXPRESSION         only run tests which match the given substring
                        expression. An expression is a python evaluatable
                        expression where all names are substring-matched
                        against test names and their parent classes. Example:
                        -k 'test_method or test other' matches all test
                        functions and classes whose name contains
                        'test_method' or 'test_other'. Additionally keywords
                        are matched to classes and functions containing extra
                        names in their 'extra_keyword_matches' set, as well as
                        functions which have names assigned directly to them.
  -m MARKEXPR           only run tests matching given mark expression.
                        example: -m 'mark1 and not mark2'.
  --markers             show markers (builtin, plugin and per-project ones).
  -x, --exitfirst       exit instantly on first error or failed test.
  --maxfail=num         exit after first num failures or errors.
  --strict              run pytest in strict mode, warnings become errors.
  -c file               load configuration from `file` instead of trying to
                        locate one of the implicit configuration files.
  --fixtures, --funcargs
                        show available fixtures, sorted by plugin appearance
  --import-mode={prepend,append}
                        prepend/append to sys.path when importing test
                        modules, default is to prepend.
  --pdb                 start the interactive Python debugger on errors.
  --capture=method      per-test capturing method: one of fd|sys|no.
  -s                    shortcut for --capture=no.
  --runxfail            run tests even if they are marked xfail
  --lf                  rerun only the tests that failed at the last run (or
                        all if none failed)
  --ff                  run all tests but run the last failures first. This
                        may re-order tests and thus lead to repeated fixture
                        setup/teardown
  --cache-show          show cache contents, don't perform collection or tests
  --cache-clear         remove all cache contents at start of test run.

reporting:
  -v, --verbose         increase verbosity.
  -q, --quiet           decrease verbosity.
  -r chars              show extra test summary info as specified by chars
                        (f)ailed, (E)error, (s)skipped, (x)failed, (X)passed
                        (w)pytest-warnings (a)all.
  -l, --showlocals      show locals in tracebacks (disabled by default).
  --report=opts         (deprecated, use -r)
  --tb=style            traceback print mode (auto/long/short/line/native/no).
  --full-trace          don't cut any tracebacks (default is to cut).
  --color=color         color terminal output (yes/no/auto).
  --durations=N         show N slowest setup/test durations (N=0 for all).
  --pastebin=mode       send failed|all info to bpaste.net pastebin service.
  --junit-xml=path      create junit-xml style report file at given path.
  --junit-prefix=str    prepend prefix to classnames in junit-xml output
  --result-log=path     path for machine-readable result log.

collection:
  --collect-only        only collect tests, don't execute them.
  --pyargs              try to interpret all arguments as python packages.
  --ignore=path         ignore path during collection (multi-allowed).
  --confcutdir=dir      only load conftest.py's relative to specified dir.
  --noconftest          Don't load any conftest.py files.
  --doctest-modules     run doctests in all .py modules
  --doctest-glob=pat    doctests file matching pattern, default: test*.txt
  --doctest-ignore-import-errors
                        ignore doctest ImportErrors

test session debugging and configuration:
  --basetemp=dir        base temporary directory for this test run.
  --version             display pytest lib version and import information.
  -h, --help            show help message and configuration info
  -p name               early-load given plugin (multi-allowed). To avoid
                        loading of plugins, use the `no:` prefix, e.g.
                        `no:doctest`.
  --trace-config        trace considerations of conftest.py files.
  --debug               store internal tracing debug information in
                        'pytestdebug.log'.
  --assert=MODE         control assertion debugging tools. 'plain' performs no
                        assertion debugging. 'reinterp' reinterprets assert
                        statements after they failed to provide assertion
                        expression information. 'rewrite' (the default)
                        rewrites assert statements in test modules on import
                        to provide assert expression information.
  --no-assert           DEPRECATED equivalent to --assert=plain
  --no-magic            DEPRECATED equivalent to --assert=plain
  --genscript=path      create standalone pytest script at given target path.

django:
  --reuse-db            Re-use the testing database if it already exists, and
                        do not remove it when the test finishes. This option
                        will be ignored when --no-db is given.
  --create-db           Re-create the database, even if it exists. This option
                        will be ignored if not --reuse-db is given.
  --ds=DS               Set DJANGO_SETTINGS_MODULE.
  --dc=DC               Set DJANGO_CONFIGURATION.
  --nomigrations        Disable Django 1.7 migrations on test setup
  --liveserver=LIVESERVER
                        Address and port for the live_server fixture.
  --fail-on-template-vars
                        Fail for invalid variables in templates.


[pytest] ini-options in the next pytest.ini|tox.ini|setup.cfg file:

  markers (linelist)       markers for test functions
  norecursedirs (args)     directory patterns to avoid for recursion
  testpaths (args)         directories to search for tests when no files or directories are given in the command line.
  usefixtures (args)       list of default fixtures to be used with this project
  python_files (args)      glob-style file patterns for Python test module discovery
  python_classes (args)    prefixes or glob names for Python test class discovery
  python_functions (args)  prefixes or glob names for Python test function and method discovery
  doctest_optionflags (args) option flags for doctests
  addopts (args)           extra command line options
  minversion (string)      minimally required pytest version
  DJANGO_CONFIGURATION (string) django-configurations class to use by pytest-django.
  DJANGO_SETTINGS_MODULE (string) Django settings module to use by pytest-django.
  django_find_project (string) Automatically find and add a Django project to the Python path.
  FAIL_INVALID_TEMPLATE_VARS (string) Fail for invalid variables in templates.


to see available markers type: py.test --markers
to see available fixtures type: py.test --fixtures
(shown according to specified file_or_dir or current dir if not specified)
{}
鈥箃empenv-73e031286076a鈥 ~ 鈫
@rnovacek

This comment has been minimized.

rnovacek commented Dec 5, 2015

It now works for me, probably fixed by some update.

pytest==2.8.2
pytest-django==2.9.1
Django==1.9

@willharris

This comment has been minimized.

willharris commented Dec 5, 2015

I still get the error. After some more digging it turns out there is something funky with the py.test plugin provided by django-assets.

[12:51:59] 馃幐 ~/tmp % virtualenv testenv                                                                                                         
New python executable in testenv/bin/python2.7
Also creating executable in testenv/bin/python
Installing setuptools, pip, wheel...done.
[12:52:08] 馃幐 ~/tmp % source testenv/bin/activate                                                                                                
(testenv)[12:52:16] 馃幐 ~/tmp % pip install pytest-django==2.9.1 pytest==2.8.1 django-assets==0.11                                                
Collecting pytest-django==2.9.1
  Using cached pytest_django-2.9.1-py2.py3-none-any.whl
Collecting pytest==2.8.1
  Using cached pytest-2.8.1-py2.py3-none-any.whl
Collecting django-assets==0.11
Collecting py>=1.4.29 (from pytest==2.8.1)
  Using cached py-1.4.31-py2.py3-none-any.whl
Collecting Django>=1.1 (from django-assets==0.11)
  Using cached Django-1.9-py2.py3-none-any.whl
Collecting webassets==0.11 (from django-assets==0.11)
Installing collected packages: py, pytest, pytest-django, Django, webassets, django-assets
Successfully installed Django-1.9 django-assets-0.11 py-1.4.31 pytest-2.8.1 pytest-django-2.9.1 webassets-0.11
(testenv)[12:52:42] 馃幐 ~/tmp % py.test -h                                                                                                        
Traceback (most recent call last):
  File "/Users/harris/tmp/testenv/bin/py.test", line 11, in <module>
    sys.exit(main())
  File "/Users/harris/tmp/testenv/lib/python2.7/site-packages/_pytest/config.py", line 48, in main
    return config.hook.pytest_cmdline_main(config=config)
  File "/Users/harris/tmp/testenv/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 724, in __call__
    return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
  File "/Users/harris/tmp/testenv/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 338, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/Users/harris/tmp/testenv/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 333, in <lambda>
    _MultiCall(methods, kwargs, hook.spec_opts).execute()
  File "/Users/harris/tmp/testenv/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 596, in execute
    res = hook_impl.function(*args)
  File "/Users/harris/tmp/testenv/lib/python2.7/site-packages/_pytest/helpconfig.py", line 59, in pytest_cmdline_main
    config._do_configure()
  File "/Users/harris/tmp/testenv/lib/python2.7/site-packages/_pytest/config.py", line 830, in _do_configure
    self.hook.pytest_configure.call_historic(kwargs=dict(config=self))
  File "/Users/harris/tmp/testenv/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 729, in call_historic
    self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
  File "/Users/harris/tmp/testenv/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 338, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/Users/harris/tmp/testenv/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 333, in <lambda>
    _MultiCall(methods, kwargs, hook.spec_opts).execute()
  File "/Users/harris/tmp/testenv/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 596, in execute
    res = hook_impl.function(*args)
  File "/Users/harris/tmp/testenv/lib/python2.7/site-packages/pytest_django/plugin.py", line 250, in pytest_configure
    _setup_django()
  File "/Users/harris/tmp/testenv/lib/python2.7/site-packages/pytest_django/plugin.py", line 134, in _setup_django
    django.setup()
  File "/Users/harris/tmp/testenv/lib/python2.7/site-packages/django/__init__.py", line 17, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/Users/harris/tmp/testenv/lib/python2.7/site-packages/django/conf/__init__.py", line 55, in __getattr__
    self._setup(name)
  File "/Users/harris/tmp/testenv/lib/python2.7/site-packages/django/conf/__init__.py", line 41, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
(testenv)[12:52:47] 馃幐 ~/tmp % pip uninstall django-assets                                                                                       
Uninstalling django-assets-0.11:
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets-0.11.dist-info/DESCRIPTION.rst
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets-0.11.dist-info/METADATA
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets-0.11.dist-info/RECORD
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets-0.11.dist-info/WHEEL
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets-0.11.dist-info/entry_points.txt
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets-0.11.dist-info/metadata.json
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets-0.11.dist-info/top_level.txt
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/__init__.py
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/__init__.pyc
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/env.py
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/env.pyc
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/filter.py
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/filter.pyc
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/finders.py
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/finders.pyc
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/glob.py
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/glob.pyc
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/loaders.py
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/loaders.pyc
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/management/__init__.py
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/management/__init__.pyc
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/management/commands/__init__.py
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/management/commands/__init__.pyc
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/management/commands/assets.py
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/management/commands/assets.pyc
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/manifest.py
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/manifest.pyc
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/models.py
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/models.pyc
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/pytest_plugin.py
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/pytest_plugin.pyc
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/settings.py
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/settings.pyc
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/templatetags/__init__.py
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/templatetags/__init__.pyc
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/templatetags/assets.py
  /Users/harris/tmp/testenv/lib/python2.7/site-packages/django_assets/templatetags/assets.pyc
Proceed (y/n)? y
  Successfully uninstalled django-assets-0.11
(testenv)[12:53:12] 馃幐 ~/tmp % py.test -h                                                                                                        
usage: py.test [options] [file_or_dir] [file_or_dir] [...]

positional arguments:
  file_or_dir

general:
  -k EXPRESSION         only run tests which match the given substring
                        expression. An expression is a python evaluatable
                        expression where all names are substring-matched
                        against test names and their parent classes. Example:
                        -k 'test_method or test other' matches all test
                        functions and classes whose name contains
                        'test_method' or 'test_other'. Additionally keywords
                        are matched to classes and functions containing extra
                        names in their 'extra_keyword_matches' set, as well as
                        functions which have names assigned directly to them.
  -m MARKEXPR           only run tests matching given mark expression.
                        example: -m 'mark1 and not mark2'.
  --markers             show markers (builtin, plugin and per-project ones).
  -x, --exitfirst       exit instantly on first error or failed test.
  --maxfail=num         exit after first num failures or errors.
  --strict              run pytest in strict mode, warnings become errors.
  -c file               load configuration from `file` instead of trying to
                        locate one of the implicit configuration files.
  --fixtures, --funcargs
                        show available fixtures, sorted by plugin appearance
  --import-mode={prepend,append}
                        prepend/append to sys.path when importing test
                        modules, default is to prepend.
  --pdb                 start the interactive Python debugger on errors.
  --capture=method      per-test capturing method: one of fd|sys|no.
  -s                    shortcut for --capture=no.
  --runxfail            run tests even if they are marked xfail
  --lf                  rerun only the tests that failed at the last run (or
                        all if none failed)
  --ff                  run all tests but run the last failures first. This
                        may re-order tests and thus lead to repeated fixture
                        setup/teardown
  --cache-show          show cache contents, don't perform collection or tests
  --cache-clear         remove all cache contents at start of test run.

reporting:
  -v, --verbose         increase verbosity.
  -q, --quiet           decrease verbosity.
  -r chars              show extra test summary info as specified by chars
                        (f)ailed, (E)error, (s)skipped, (x)failed, (X)passed
                        (w)pytest-warnings (a)all.
  -l, --showlocals      show locals in tracebacks (disabled by default).
  --report=opts         (deprecated, use -r)
  --tb=style            traceback print mode (auto/long/short/line/native/no).
  --full-trace          don't cut any tracebacks (default is to cut).
  --color=color         color terminal output (yes/no/auto).
  --durations=N         show N slowest setup/test durations (N=0 for all).
  --pastebin=mode       send failed|all info to bpaste.net pastebin service.
  --junit-xml=path      create junit-xml style report file at given path.
  --junit-prefix=str    prepend prefix to classnames in junit-xml output
  --result-log=path     path for machine-readable result log.

collection:
  --collect-only        only collect tests, don't execute them.
  --pyargs              try to interpret all arguments as python packages.
  --ignore=path         ignore path during collection (multi-allowed).
  --confcutdir=dir      only load conftest.py's relative to specified dir.
  --noconftest          Don't load any conftest.py files.
  --doctest-modules     run doctests in all .py modules
  --doctest-glob=pat    doctests file matching pattern, default: test*.txt
  --doctest-ignore-import-errors
                        ignore doctest ImportErrors

test session debugging and configuration:
  --basetemp=dir        base temporary directory for this test run.
  --version             display pytest lib version and import information.
  -h, --help            show help message and configuration info
  -p name               early-load given plugin (multi-allowed). To avoid
                        loading of plugins, use the `no:` prefix, e.g.
                        `no:doctest`.
  --trace-config        trace considerations of conftest.py files.
  --debug               store internal tracing debug information in
                        'pytestdebug.log'.
  --assert=MODE         control assertion debugging tools. 'plain' performs no
                        assertion debugging. 'reinterp' reinterprets assert
                        statements after they failed to provide assertion
                        expression information. 'rewrite' (the default)
                        rewrites assert statements in test modules on import
                        to provide assert expression information.
  --no-assert           DEPRECATED equivalent to --assert=plain
  --no-magic            DEPRECATED equivalent to --assert=plain
  --genscript=path      create standalone pytest script at given target path.

django:
  --reuse-db            Re-use the testing database if it already exists, and
                        do not remove it when the test finishes. This option
                        will be ignored when --no-db is given.
  --create-db           Re-create the database, even if it exists. This option
                        will be ignored if not --reuse-db is given.
  --ds=DS               Set DJANGO_SETTINGS_MODULE.
  --dc=DC               Set DJANGO_CONFIGURATION.
  --nomigrations        Disable Django 1.7 migrations on test setup
  --liveserver=LIVESERVER
                        Address and port for the live_server fixture.
  --fail-on-template-vars
                        Fail for invalid variables in templates.


[pytest] ini-options in the next pytest.ini|tox.ini|setup.cfg file:

  markers (linelist)       markers for test functions
  norecursedirs (args)     directory patterns to avoid for recursion
  testpaths (args)         directories to search for tests when no files or directories are given in the command line.
  usefixtures (args)       list of default fixtures to be used with this project
  python_files (args)      glob-style file patterns for Python test module discovery
  python_classes (args)    prefixes or glob names for Python test class discovery
  python_functions (args)  prefixes or glob names for Python test function and method discovery
  doctest_optionflags (args) option flags for doctests
  addopts (args)           extra command line options
  minversion (string)      minimally required pytest version
  DJANGO_CONFIGURATION (string) django-configurations class to use by pytest-django.
  DJANGO_SETTINGS_MODULE (string) Django settings module to use by pytest-django.
  django_find_project (string) Automatically find and add a Django project to the Python path.
  FAIL_INVALID_TEMPLATE_VARS (string) Fail for invalid variables in templates.


to see available markers type: py.test --markers
to see available fixtures type: py.test --fixtures
(shown according to specified file_or_dir or current dir if not specified)
{}
(testenv)[12:53:15] 馃幐 ~/tmp % 
@willharris

This comment has been minimized.

willharris commented Dec 5, 2015

And perhaps just to clarify, I'm not 100% sure this is an issue with pytest-django anymore, but does seem so to me:

[13:00:16] 馃幐 ~/tmp % virtualenv testenv                                                                                                         
New python executable in testenv/bin/python2.7
Also creating executable in testenv/bin/python
Installing setuptools, pip, wheel...done.
[13:00:22] 馃幐 ~/tmp % source testenv/bin/activate                                                                                                
(testenv)[13:00:26] 馃幐 ~/tmp % pip install pytest==2.8.1 django-assets==0.11                                                                     
Collecting pytest==2.8.1
  Using cached pytest-2.8.1-py2.py3-none-any.whl
Collecting django-assets==0.11
Collecting py>=1.4.29 (from pytest==2.8.1)
  Using cached py-1.4.31-py2.py3-none-any.whl
Collecting Django>=1.1 (from django-assets==0.11)
  Using cached Django-1.9-py2.py3-none-any.whl
Collecting webassets==0.11 (from django-assets==0.11)
Installing collected packages: py, pytest, Django, webassets, django-assets
Successfully installed Django-1.9 django-assets-0.11 py-1.4.31 pytest-2.8.1 webassets-0.11
(testenv)[13:00:53] 馃幐 ~/tmp % py.test -h                                                                                                        
usage: py.test [options] [file_or_dir] [file_or_dir] [...]

positional arguments:
  file_or_dir

general:
  -k EXPRESSION         only run tests which match the given substring
                        expression. An expression is a python evaluatable
                        expression where all names are substring-matched
                        against test names and their parent classes. Example:
                        -k 'test_method or test other' matches all test
                        functions and classes whose name contains
                        'test_method' or 'test_other'. Additionally keywords
                        are matched to classes and functions containing extra
                        names in their 'extra_keyword_matches' set, as well as
                        functions which have names assigned directly to them.
  -m MARKEXPR           only run tests matching given mark expression.
                        example: -m 'mark1 and not mark2'.
  --markers             show markers (builtin, plugin and per-project ones).
  -x, --exitfirst       exit instantly on first error or failed test.
  --maxfail=num         exit after first num failures or errors.
  --strict              run pytest in strict mode, warnings become errors.
  -c file               load configuration from `file` instead of trying to
                        locate one of the implicit configuration files.
  --fixtures, --funcargs
                        show available fixtures, sorted by plugin appearance
  --import-mode={prepend,append}
                        prepend/append to sys.path when importing test
                        modules, default is to prepend.
  --pdb                 start the interactive Python debugger on errors.
  --capture=method      per-test capturing method: one of fd|sys|no.
  -s                    shortcut for --capture=no.
  --runxfail            run tests even if they are marked xfail
  --lf                  rerun only the tests that failed at the last run (or
                        all if none failed)
  --ff                  run all tests but run the last failures first. This
                        may re-order tests and thus lead to repeated fixture
                        setup/teardown
  --cache-show          show cache contents, don't perform collection or tests
  --cache-clear         remove all cache contents at start of test run.

reporting:
  -v, --verbose         increase verbosity.
  -q, --quiet           decrease verbosity.
  -r chars              show extra test summary info as specified by chars
                        (f)ailed, (E)error, (s)skipped, (x)failed, (X)passed
                        (w)pytest-warnings (a)all.
  -l, --showlocals      show locals in tracebacks (disabled by default).
  --report=opts         (deprecated, use -r)
  --tb=style            traceback print mode (auto/long/short/line/native/no).
  --full-trace          don't cut any tracebacks (default is to cut).
  --color=color         color terminal output (yes/no/auto).
  --durations=N         show N slowest setup/test durations (N=0 for all).
  --pastebin=mode       send failed|all info to bpaste.net pastebin service.
  --junit-xml=path      create junit-xml style report file at given path.
  --junit-prefix=str    prepend prefix to classnames in junit-xml output
  --result-log=path     path for machine-readable result log.

collection:
  --collect-only        only collect tests, don't execute them.
  --pyargs              try to interpret all arguments as python packages.
  --ignore=path         ignore path during collection (multi-allowed).
  --confcutdir=dir      only load conftest.py's relative to specified dir.
  --noconftest          Don't load any conftest.py files.
  --doctest-modules     run doctests in all .py modules
  --doctest-glob=pat    doctests file matching pattern, default: test*.txt
  --doctest-ignore-import-errors
                        ignore doctest ImportErrors

test session debugging and configuration:
  --basetemp=dir        base temporary directory for this test run.
  --version             display pytest lib version and import information.
  -h, --help            show help message and configuration info
  -p name               early-load given plugin (multi-allowed). To avoid
                        loading of plugins, use the `no:` prefix, e.g.
                        `no:doctest`.
  --trace-config        trace considerations of conftest.py files.
  --debug               store internal tracing debug information in
                        'pytestdebug.log'.
  --assert=MODE         control assertion debugging tools. 'plain' performs no
                        assertion debugging. 'reinterp' reinterprets assert
                        statements after they failed to provide assertion
                        expression information. 'rewrite' (the default)
                        rewrites assert statements in test modules on import
                        to provide assert expression information.
  --no-assert           DEPRECATED equivalent to --assert=plain
  --no-magic            DEPRECATED equivalent to --assert=plain
  --genscript=path      create standalone pytest script at given target path.


[pytest] ini-options in the next pytest.ini|tox.ini|setup.cfg file:

  markers (linelist)       markers for test functions
  norecursedirs (args)     directory patterns to avoid for recursion
  testpaths (args)         directories to search for tests when no files or directories are given in the command line.
  usefixtures (args)       list of default fixtures to be used with this project
  python_files (args)      glob-style file patterns for Python test module discovery
  python_classes (args)    prefixes or glob names for Python test class discovery
  python_functions (args)  prefixes or glob names for Python test function and method discovery
  doctest_optionflags (args) option flags for doctests
  addopts (args)           extra command line options
  minversion (string)      minimally required pytest version


to see available markers type: py.test --markers
to see available fixtures type: py.test --fixtures
(shown according to specified file_or_dir or current dir if not specified)
{}
(testenv)[13:00:55] 馃幐 ~/tmp % 
@pelme

This comment has been minimized.

Member

pelme commented Dec 5, 2015

django-assets ships with its own pytest plugin:

https://github.com/miracle2k/django-assets/blob/d32a7d8ca59555e0d6f452cd18ccd6e01e3211f6/setup.py#L61-L66

that includes a single fixture that is marked for autouse:

https://github.com/miracle2k/django-assets/blob/d32a7d8ca59555e0d6f452cd18ccd6e01e3211f6/django_assets/pytest_plugin.py#L4-L7

By importing django_assets.env (which happens via the pytest plugin definition), django gets imported and pytest-django gets confused by this since it looks like Django has been properly imported.

I have not investigated this further, but generally, stuff from within Django must not be imported before before settings and django.setup() is configured. django-assets should probably play nicer with Django by using the ready() hooks.

@pelme pelme closed this in 211495b Dec 5, 2015

@pelme

This comment has been minimized.

Member

pelme commented Dec 5, 2015

I've added a fix for this in 211495b.

This error also appeared in pytest-django's own test suite since it has a conftest.py that does from django.conf import settings.

Buuut, in the case with django-assets: there is some import magic going on. It may be buggy and give surprising errors by not using Django 1.7+ app loading APIs.

@willharris

This comment has been minimized.

willharris commented Dec 5, 2015

Great stuff, thanks for this @pelme!

tomviner added a commit to tomviner/pytest-django that referenced this issue Jun 22, 2016

Be more careful to not load Django when settings is not properly
configured.

When another plugin(or conftest) imports Django, there might be errors
arising. Importing Django before pytest-django has the chance to
configure settings and call django.setup() may be an error, but it may
also be correct depending on what gets imported.

This commit closes pytest-dev#289.

mfa added a commit to aexeagmbh/pytest-django that referenced this issue May 17, 2017

Be more careful to not load Django when settings is not properly
configured.

When another plugin(or conftest) imports Django, there might be errors
arising. Importing Django before pytest-django has the chance to
configure settings and call django.setup() may be an error, but it may
also be correct depending on what gets imported.

This commit closes pytest-dev#289.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment