what are the differences between "python setup.py test" vs "pytest {package}"?

Question:

I am building a continuous integration workflow using GitHub actions. This workflow will be used to run unit tests and test build status. The workflow will be used by a number of different people who have developed python package so I am trying to make it as general and modular as possible. My goal is to seperate the tasks of running unit tests and testing the build status of these packages, which leads me to my question, what are the differences between the commands python setup.py test and pytest {package}?

Specifically,

  1. Does running unit tests with the command pytest {package} automatically build your package or is it not possible to run unit tests without building your package?

  2. I know that setup.py is the build script for setuptools and I have read what the command python setup.py test does and it says it will: run unit tests after in-place build. So does this command use pytest by default then?

I also saw in the setuptools docs the build command, so to take a stab at answering my own questions, I would do pytest {package} for running unit tests only and python setup.py build to test the build status of the package.

Is that correct?

Thank you very much.

Asked By: ben stear

||

Answers:

Doesn’t answer about the differences, but…

Warning: test is deprecated and will be removed in a future version. Users looking for a generic test entry point independent of test runner are encouraged to use tox.

setuptools documentation on the command "test – Build package and run a unittest suite"

Answered By: sinoroc

Does running unit tests with the command pytest {package}
automatically build your package?

No, it doesn’t, and you don’t need it to – you don’t need the package to run your tests.

Is it not possible to run unit tests without building your package?

It is possible.

I know that setup.py is the build script for setuptools and I have
read what the command python setup.py test does and it says it will:
run unit tests after in-place build. So does this command use pytest
by default then?

It does run the configured tests, so this depends on your setup. Running python setup.py test is deprecated as already mentioned, so you usually don’t have to worry about this – use pytest directly in your CI environment. Note though that some Linux distros still do run this by default on deployment, so if you want to distribute your package you may still have to test that.

To run pytest via setup you probably need something like this in your setup.py:

setup(
    packages=find_packages(),
    tests_require=['pytest'],
)
Answered By: MrBean Bremen