Is there a way to rerun all Pytest tests when a file is saved? - pytest

Is there a way to automatically re-run all tests with Pytest if a file is saved in the project?
This is essentially Jest's --watch and --watchAll flags, which respectively reruns the designated test file and all test files whenever any files are saved.
Does PyTest have a similar function or flag?

If it is not too late, I have just found out about pytest-xdist
You can install it with $ pip install pytest-xdist
And run the test watcher with $ pytest -f and it looks at your directory and runs the test when files change.
More info here https://pytest-xdist.readthedocs.io/en/latest/index.html

Related

Pytest does not find my tests in Poetry project (VSCode finds)

I've just created my first Python package using Poetry using the usual poetry new mypackage command. My problem is that pytest does not execute any test when I run it. I'm developing using VSCode and the weird behavior is that VSCode successfully finds and executes my tests.
Poetry created a subdir called mypackage and another called tests. My test file is called tests/test_mypackage.py.
VSCode autodiscoverd the tests, and display them in the test tab. The .vscode/settings.json file has this configuration:
"python.testing.pytestArgs": [
"tests"
],
I've tried the following commands to execute pytest:
With my venv manually activated:
pytest
pytest tests
pytest tests/test_mypackage.py
cd tests;pytest test_mypackage.py
without my venv activated:
poetry run pytest
poetry run pytest tests
The behavior is always the same: nothing happens, as if pytest couldn't detect anything to run.
I've been using VSCode to run the tests, but now I want to put the code under Continuous Integration. How do I run pytest to validate my package?
UPDATE: from inside the virtual env pytest does not prints any output when run, but its return code is 1.
Poetry doesn't require to activate the venv. The command should work:
poetry run pytest
Though, you can activate the venv (with poetry shell) and run just pytest.
For the Test Explorer, I think this is still on development, officially, only Pytest and Unittest are supported, but the VSCode suggested a variable to support Poetry.
Here are the configurations that have worked to me:
{
"python.testing.cwd": ".",
"python.poetryPath": "poetry",
"python.testing.pytestEnabled": true,
"python.testing.pytestPath": ".venv/bin/pytest",
"python.testing.autoTestDiscoverOnSaveEnabled": true,
}
If you additionally needs to use environment variables, set them up in a .env file (it would be better a .env.test, but I couldn't figure it out).
pytest still does not work when running within the activated virtual environment, but I discovered that I can execute it running:
poetry run pytest
I still do not understand why it can't find the test when directly run from the command line even with the venv activated.

vscode tests discovery with poetry (src layout)

Last time I've followed recommended src layout (https://hynek.me/articles/testing-packaging/) with using tox with great success.
However VSCODE tests discovery fails because src package cannot be imported. That is expected as we want to test installed package.
But how to debug my tests in vscode?
(Q author here: I've done research on that before posting the question, so sharing what I found)
Not solution
You could modify your PYTHONPATH to point to your src directory, but it breaks the main benefit from having separate src directory (read the link from OP).
Solution
Use pip install -e path/to/your/package (usually pip install -e .) to enable development mode and test versus your codebase as it would be installed.
After that your tests should be discovered properly. Otherwise it is different issue - read vs code OUTPUT console.
Note: this requires setup.py as a build backend
workaround for poetry
pyproject.toml
[build-system]
requires = [
"poetry-core>=1.0.0",
"setuptools" # to support local installations
]
then
poetry build --format sdist && tar --wildcards -xvf dist/*.tar.gz -O '*/setup.py' > setup.py
pip install -e .
Source: https://github.com/python-poetry/poetry/issues/34
TLDR: proper solution is outside of poetry scope, links to python-list discussions: https://github.com/python-poetry/poetry/issues/34#issuecomment-732478605

Cannot run 'make' in VScode Terminal

For a school assignment, I am trying to compile a C file using a provided Makefile in Vscode. The makefile contains the following:
CFLAGS += -std=gnu11 -g
EXES = greet
all: $(EXES)
clean:
rm -f $(EXES)
greet: greet.c
# don't treat all and clean as file targets
.PHONY: all clean
When I run make in the VScode terminal, it gives me:
bash: make: command not found
Why is this happening? The assignment says this:
The accompanying Makefile will build the program greet. Thus, you can compile the
program by running make. The make program will print out each command that it uses to
compile the program. Note that if you run make twice in a row, the second time it won’t do
anything, because it knows your source file hasn’t changed.
Run the program using the following command:
./greet
I don't know if this has anything to do with my tasks.json file in VScode?
I also ran across this VSCode extension: https://naereen.github.io/Makefiles-support-for-VSCode/
It says Vscode now has something built-in: https://github.com/microsoft/vscode/tree/master/extensions/make
I don't know how to install this.

Is it possible to point tox to pull a dependency from a branch (aka use `pip -e` behind the scenes)?

How to test on py27 and py37 in tox when the py37 changes aren't packaged to pypi
The py3.7 compatible changes exist in repo branches.
They can be run by hand through pip -e installing them and running pytest without tox.
I'd like to move to running them through tox, but I can't figure out the correct string to give the deps list, or perhaps this is done in another way.
Attempted solution:
tox.ini
[tox]
envlist = py27,py37
[testenv:py27]
deps =
pytest
pytest-cov
pytest-mock
pylint
; packages specified by the setup.py cover the other dependencies for py2.7
commands =
pytest -v
[testenv:py37]
deps =
pytest
pytest-cov
pytest-mock
pylint
git+ssh//repo_url/location1.git#branchname_that_supports_py37
git+ssh//repo_url/location2.git#branchname_that_supports_py37
git+ssh//repo_url/location3.git#branchname_that_supports_py37
git+ssh//repo_url/location4.git#branchname_that_supports_py37
git+ssh//repo_url/location5.git#branchname_that_supports_py37
git+ssh//repo_url/location6.git#branchname_that_supports_py37
git+ssh//repo_url/location7.git#branchname_that_supports_py37
git+ssh//repo_url/location8.git#branchname_that_supports_py37
commands =
pytest -v
For VCS URLs pip needs to know the name of the package that should be provided with #egg=name:
git+ssh//repo_url/location1.git#branchname_that_supports_py37#egg=package1
Otherwise your tox.ini looks good. I use the same approach, for example.

Using Sails.js with yarn

When I run sails new myapp it generates a package.json file and a /node_modules directory. The sails docs say to cd in and run npm install to get up and running.
I've recently started using yarn and would like to use it to manage the additional dependencies I add to my sails app. However, when I run yarn init (what I think creates the yarn.lock file) it looks like it wants to create my package.json again. And it errors on the entry point question, saying Cannot convert object to primitive value.
Should I just stick to vanilla npm? Can Yarn and Sails play nicely in the sandbox together and share the toys?
In fact, $ yarn init (as well as $ npm init) will just initialize your package.json file by asking a few questions.
Here you don't need to regenerate your package.json but just install your node modules and generate a yarn.lock file to lock your modules versions. You can do this by using $ yarn install or just $ yarn.
You can skip $ yarn init as it does the exact same thing as $ npm init. $ yarn install actually creates the yarn.lock file. Unlike npm, yarn actually respects the engines property in a package.json file. If you run into this you can use $ yarn install --ignore-engines.