Feature request: Enable pytest for executable scripts without ".py" extension
See original GitHub issueFirst of all, let me say that I love pytest! Thanks for your great work.
As far as I understand it is considered standard practice to name executable python scripts without the “.py” extension (e.g., pip
, pytest
, etc.) - at least on linux. Normally such scripts do not contain much code, but it might still be useful to be able to test them with pytest, which is currently not possible.
Test case:
pyenv virtualenv 3.6.5 foo
pyenv activate foo
pip install pytest
echo "#/usr/bin/env python python\n\ndef test_bla():\n assert True" > ./foo
pytest ./foo
Actual result:
============================================================================== test session starts ==============================================================================
platform linux -- Python 3.6.5, pytest-3.6.0, py-1.5.3, pluggy-0.6.0
rootdir: /home/duerpp, inifile:
collecting 0 items
========================================================================= no tests ran in 0.00 seconds ==========================================================================
ERROR: not found: /home/duerrp/foo
(no name '/home/duerrp/foo' in any of [])
Expected:
============================================================================== test session starts ==============================================================================
platform linux -- Python 3.6.5, pytest-3.6.0, py-1.5.3, pluggy-0.6.0
rootdir: /home/duerrp, inifile:
collected 1 item
foo . [100%]
=========================================================================== 1 passed in 0.01 seconds ============================================================================
The expected behavior could be implemented using a plugin, but I personally think it should be solved by pytest itself…
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:9 (6 by maintainers)
Top Results From Across the Web
How to execute python scripts with a custom name without .py ...
1 Answer 1 ... Assuming you're on a unixoid system, you just need to add a shebang line: #!/usr/local/bin/python import requests ... The...
Read more >Testing Python in Visual Studio Code
Python testing in Visual Studio Code. The Python extension supports testing with Python's built-in unittest framework and pytest.
Read more >API Reference — pytest documentation
Import and return the requested module modname , or skip the current test if the module cannot ... Run a python script using...
Read more >Running and Writing Tests - Python Developer's Guide
If you want to run a single test file, simply specify the test file name (without the extension) as an argument. You also...
Read more >pytest Documentation - Read the Docs
The fixtures in pytest request fixtures just like ... nose2pytest is a Python script and pytest plugin to help convert Nose-based tests into ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
okay, so i should follow #4476 is what you’re saying? 😃
I am constantly writing small scripts that I don’t want to bother to build an entire package for. They’re just scripts: I drop them in
~/bin/foo
and they work. The only reasonpytest
can’t run them is because they don’t have a.py
extension.I currently work around this problem by creating a hardlink to the script (a symlink doesn’t work for some reason), but that’s really just silly:
This shouldn’t be necessary… I understand you want to have this be a plugin but I don’t really know where to start to write one for this. And I did read the documentation on how to write plugins - it’s unclear to me that I could actually change the “this is not a module” behavior at all from those examples.
I’m aware of the existence of pytest-console-scripts, but that’s not exactly what I want here: I don’t want to test the program as a script: I have real
test_foo
functions in there that are properly designed and are detected bypytest
when the file has the right extension.I find this problem especially confusing because the
pytest
commandline usage says this:… ie. that it should just be able to work directly on a file (not a module, mind you, a file).
Is there some magic trick I’m missing here or I’m the only idiot out there writing tests for his small silly non-packaged programs? 😃