Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking script methods when testing with pytest-console-scripts #44

Closed
nodje opened this issue Mar 18, 2021 · 3 comments
Closed

Mocking script methods when testing with pytest-console-scripts #44

nodje opened this issue Mar 18, 2021 · 3 comments

Comments

@nodje
Copy link

nodje commented Mar 18, 2021

Hi,

i've started using your package to test some legacy code I need to refactor.

Took me a while to figure out an essential part: how to mock functions called by the script.
I think you should write a comment on this part in the Readme, as this appear to be a common use case for your PCS.

So far, tell me if I'm wrong, it's possible to mock function when the script is called as
"inprocess". If it's called as subprocess, it's not possible to mock, afaics.

The problem I'm facing right now is that I can't find a way to mock calls to functions internal to the script. Maybe it's just not possible in python.

FYI https://stackoverflow.com/questions/66676962/how-to-mock-a-function-within-a-python-script-tested-with-pytest-console-scripts

@kvas-it
Copy link
Owner

kvas-it commented Mar 18, 2021

So far, tell me if I'm wrong, it's possible to mock function when the script is called as
"inprocess". If it's called as subprocess, it's not possible to mock, afaics.

Yes, you are exactly right. When the script is called as a subprocess we can't mock, because the script is a separate process and the test doesn't have access to its namespaces.

The problem I'm facing right now is that I can't find a way to mock calls to functions internal to the script. Maybe it's just not possible in python.

Here you are right again, but there are ways around it. They are not perfect, but it's something. I replied to you on stackoverflow.

@nodje
Copy link
Author

nodje commented Mar 18, 2021

Great, you made my day!
Your inprocess run approach is definitely super interesting to be able to blackbox/integration test a script (and thus mocking externalities, which is necessary in most cases)

Actually, I can't see anything else at the moment that would allow mocking function for a python script.

You should def. emphasize it in your Readme/doc

@nodje nodje closed this as completed Mar 18, 2021
@kvas-it
Copy link
Owner

kvas-it commented Mar 18, 2021

Yeah, I think you are right that ability to mock things inside of scripts is an important feature. I will merge the PR that adds this to the README. Let me know if you see a better way to describe it. Thanks for the proposal and for kind words.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants