Skip to content
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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

elpy runs tests in directory other than root #1720

Open
TobyHFerguson opened this issue Nov 19, 2019 · 2 comments
Open

elpy runs tests in directory other than root #1720

TobyHFerguson opened this issue Nov 19, 2019 · 2 comments

Comments

@TobyHFerguson
Copy link

Summary

Thank you for reporting to Elpy !
I've configured my python system so that (elpy-project-root) is what I want it to be, but when I go run my tests elpy's default directory is the tests/ directory, not the project root, so it can't import the necessary files.

Steps to reproduce

run a test in a python test file - tests are in a directory parallel to the main body:

(productcatalog) bash-5.0$ tree
.
├── productcatalog
│   ├── __init__.py
│   ├── app.py
│   ├── category.py
│   ├── image.py
│   └── product.py
├── setup.py
└── tests
    ├── conftest.py
    ├── test_category.py
    ├── test_image.py
    ├── test_integration.py
    └── test_product.py

But when I run the tests (from test_category.py), for example, I get:

-*- mode: compilation; default-directory: "~/Development/ProductCatalog/tests/" -*-
Compilation started at Mon Nov 18 16:52:00

python -m pytest /Users/tobyferguson/Development/ProductCatalog/tests/test_image.py\:\:test_remove_image
======================= test session starts =======================
platform darwin -- Python 3.8.0, pytest-5.2.2, py-1.8.0, pluggy-0.13.0
rootdir: /Users/tobyferguson/Development/ProductCatalog
collected 0 items / 1 errors                                      

============================= ERRORS ==============================
______________ ERROR collecting tests/test_image.py _______________
ImportError while importing test module '/Users/tobyferguson/Development/ProductCatalog/tests/test_image.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
test_image.py:1: in <module>
    from productcatalog import create_image, get_image, image_exists, remove_image
E   ModuleNotFoundError: No module named 'productcatalog'

My configuration

OS

Mac OS 10.14.6 (Mojave)

Result of (elpy-config)

Elpy Configuration

Emacs.............: 26.3
Elpy..............: 1.31.0
Virtualenv........: productcatalog (/Users/tobyferguson/.virtualenvs/productcatalog)
Interactive Python: python 3.8.0 (/Users/tobyferguson/.virtualenvs/productcatalog/bin/python)
RPC virtualenv....: rpc-venv (/Users/tobyferguson/.emacs.d/elpy/rpc-venv)
 Python...........: python 3.8.0 (/Users/tobyferguson/.emacs.d/elpy/rpc-venv/bin/python)
 Jedi.............: 0.15.1
 Rope.............: 0.14.0
 Autopep8.........: 1.4.4
 Yapf.............: 0.28.0
 Black............: 19.10b0
Syntax checker....: Not found (flake8)

Warnings

The configured syntax checker could not be found. Elpy uses this
program to provide syntax checks of your programs, so you might want
to install one. Elpy by default uses flake8.

[Install flake8]

Options

`Raised' text indicates buttons; type RET or click mouse-1 on a button
to invoke its action.  Invoke [+] to expand a group, and [-] to
collapse an expanded group.  Invoke the [Group], [Face], and [Option]
buttons below to edit that item in another window.

[+]-- Group Elpy
[+]-- Group Python
[+]-- Group Virtual Environments (Pyvenv)
[+]-- Group Completion (Company)
[+]-- Group Call Signatures (ElDoc)
[+]-- Group Inline Errors (Flymake)
[+]-- Group Snippets (YASnippet)
[+]-- Group Directory Grep (rgrep)
[+]-- Group Search as You Type (ido)
[+]-- Group Django extension
[+]-- Group Autodoc extension

Elpy configuration in my init.el

 (use-package blacken)
  (use-package elpy
    :ensure t
    :defer t
    :init
    (advice-add 'python-mode :before 'elpy-enable)
    :config
    (when (require 'flycheck nil t)
      (setq elpy-modules (delq 'elpy-module-flymake elpy-modules))
      (add-hook 'elpy-mode-hook 'flycheck-mode))
    (setq elpy-test-pytest-runner-command  '("python -m pytest"))
    (setq elpy-test-runner 'elpy-test-pytest-runner)
    )
@TobyHFerguson
Copy link
Author

I stepped out for a few minutes and had the idea to run pip install -e .. Once I'd done that then elpy started to work as expected. I haven't figured it out yet, but that fixed the issue. It might be worth a note in the docs that this might be the problem, for other poor souls like me who spend an hour or so trying to get elpy to work :-(

@galaunay
Copy link
Collaborator

pytest needs you to have the package installed (see here).

So even with the good default directory (which will be fixed), it will not work.

You can also add a __init__.py file in your tests directory.
It is not documented in pytest doc, but it somehow works...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants