Comments (5)
It should work in in-process mode but not in subprocess mode (because in that case you're starting a separate process so the mocking won't apply). To ensure your test runs in in-process mode you can add this decorator to it:
@pytest.mark.script_launch_mode('inprocess')
Let me know if this doesn't work. This would be a bug that I'd want to fix.
from pytest-console-scripts.
Isn't inprocess
the default launch mode? In any case, it still does not work when I set it explicitly
from pytest-console-scripts.
Yeah, you're right, mocking in the script itself doesn't work even in in-process mode. The reason for that is that the script is not imported via the usual module machinery but is compiled and executed directly instead. There are two problems with importing it as a module:
- it might not be accessible (not in the path and/or not a
.py
file), - if we import it as a module,
__name__
will not be"__main__"
, soif __name__ == '__main__':
block that many scripts have would not execute, which would mess things up.
It's possible to work around (1), at least we could use the module if it is importable, but I don't see a way to work around (2) without lots of hacks that I'd rather not go into.
If it helps, you can import other modules into the script and mock objects in those modules. Any module that you can import
into the script and into the test you can mock (and I added a test for this now), just not the script itself.
from pytest-console-scripts.
I figured it was a hard limitation. Thanks for the quick response!
Also, great job on the plugin π
from pytest-console-scripts.
Also, great job on the plugin π
Thank you!
from pytest-console-scripts.
Related Issues (20)
- UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcb in position 24: invalid continuation byte HOT 8
- Running scripts that have not been installed HOT 10
- Encoding issues under Windows HOT 12
- extremely slow execution of script tests HOT 3
- Add option to suppress printing RunResult on init HOT 8
- Cross OS support? HOT 11
- Mocking script methods when testing with pytest-console-scripts HOT 3
- Strange behaviour with Pytest when running script is imported as module in another test file. HOT 4
- print_result='on_fail' HOT 9
- βpython_requiresβ should be set with β>=3.6β, as pytest-console-scripts 1.2.2 is not compatible with all Python versions. HOT 1
- PyPI Wheels? HOT 3
- 1.3.1: missing install-time dependencies HOT 5
- Test fails when called alone, succeeds with whole test module HOT 8
- Testing scripts which trigger user input HOT 4
- script_runner fixture type-hint. HOT 2
- Parity with subprocess.run HOT 3
- thank you for writing pytest-console-scripts
- Decode error when using inprocess tests on windows 10 HOT 1
- `pytest-console-scripts` causes `pytest-xdist` tests to fail HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pytest-console-scripts.