question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: Enable pytest for executable scripts without ".py" extension

See original GitHub issue

First 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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
anarcatcommented, Sep 23, 2019

okay, so i should follow #4476 is what you’re saying? 😃

1reaction
anarcatcommented, Sep 20, 2019

this should absolutely be a plugin - as far as consistent python packaging is concerned - scripts come from entrypoints of modules and there is no excuse not to have a test module

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 reason pytest 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:

anarcat@angela:bin(master)$ ln -sf spaced spaced.py
anarcat@angela:bin(master)$ pytest-3 spaced
============================= test session starts ==============================
platform linux -- Python 3.7.3, pytest-3.10.1, py-1.7.0, pluggy-0.8.0
rootdir: /home/anarcat/bin, inifile:
plugins: remotedata-0.3.1, openfiles-0.3.2, doctestplus-0.2.0, cov-2.6.0, arraydiff-0.3, betamax-0.8.1
collecting ... 
========================= no tests ran in 0.02 seconds =========================
ERROR: not found: /home/anarcat/bin/spaced
(no name '/home/anarcat/bin/spaced' in any of [])

[4]anarcat@angela:bin(master)$ pytest-3 spaced.py 
============================= test session starts ==============================
platform linux -- Python 3.7.3, pytest-3.10.1, py-1.7.0, pluggy-0.8.0
rootdir: /home/anarcat/bin, inifile:
plugins: remotedata-0.3.1, openfiles-0.3.2, doctestplus-0.2.0, cov-2.6.0, arraydiff-0.3, betamax-0.8.1
collecting ... 
========================= no tests ran in 0.02 seconds =========================
ERROR: not found: /home/anarcat/bin/spaced
(no name '/home/anarcat/bin/spaced' in any of [])

[4]anarcat@angela:bin(master)$ ln -f spaced spaced.py
anarcat@angela:bin(master)$ pytest-3 spaced.py 
============================= test session starts ==============================
platform linux -- Python 3.7.3, pytest-3.10.1, py-1.7.0, pluggy-0.8.0
rootdir: /home/anarcat/bin, inifile:
plugins: remotedata-0.3.1, openfiles-0.3.2, doctestplus-0.2.0, cov-2.6.0, arraydiff-0.3, betamax-0.8.1
collected 2 items                                                              

spaced.py ..                                                             [100%]

=========================== 2 passed in 0.04 seconds ===========================

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 by pytest when the file has the right extension.

I find this problem especially confusing because the pytest commandline usage says this:

$ pytest-3 --help
usage: pytest-3 [options] [file_or_dir] [file_or_dir] [...]

positional arguments:
  file_or_dir

… 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? 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found