python unittest - patch an entire import [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Our product imports the library winreg.
I am writing a test which I would like to mock the entire library.
I do not want to mock specific functions, nor enumerate through all of the functions and mock them. I would like to mock the entire library, such that any instance of the library being imported or used instead uses the mock.
Is this possible?

Have you tried patch?
It is not necessarily intuitive to setup though, Python doc example:
#patch('mymodule.SomeClass')
class MyTest(TestCase):

This is a tricky problem. I think you're best bet is to have your real library "winreg" in one folder location and your mock library in another location and do some sneaky PYTHONPATH manipulation when you run the unit tests.
Folder structure and assuming winreg is a python module but could still work if a folder.
real/winreg.py
mock/winreg.py
When you execute your unit tests if on a Linux system:
PYTHONPATH=<directory to mock>/mock:$PYTHONPATH <command to run unittests>
When you run the normal code flip the PYTHONPATH back to the "real" library. You'll just have to make sure that the real library isn't picked up in your PYTHONPATH when running the unit tests which could be tricky depending on your set up.

Related

Can Python create a self contained package that can be deployed where python and pip is not installed? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Some programming languages provide capability t create a self contained packages that can run on any machine.
For example, dotnet core can self-contained apps per below:
https://learn.microsoft.com/en-us/dotnet/core/deploying/#publish-self-contained
The C# self-contained apps can be single large file, or a directory of all files required to run the application. The package can target Linux, mac or Windows.
In Python, what is the closest feature to self-contained app packages described above?
PyInstaller seems to be the current go to, and it works well in my experience. However, some people have reported that it has very large file sizes, but I've personally never found that to be a major issue.
If you use that, you would also probably need some kind of UI, but that's a separate issue in itself.

Best Way to Import External Python Project [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
The project I am working on essentially benchmarks certain aspects of various external python projects. This is easy to do for "official" or "well-supported" projects which allow installing as a module. However, I now need to extend this to cases where the projects are messy or have minimal support and were never expected to be used as modules.
My code needs to import certain classes from these projects. These projects often assume that they are the main piece of code, e.g. if they have a subfolder called "utils" they assume they can just import utils and go about their business. There's a lot of these imports so it's not really feasible to change them all.
Making as few changes as possible to the 3rd party projects, whats the best way to import code from them?
Make sure that all folders are proper Python packages so imports don't fail e.g. create each of the required __init__.py files, then write a setup.py file to bundle them as a library (more information here).
Install the library wherever you want (virtualenv recommended) using pip install path_to_project_folder.
Then import them using the name you've chosen in your setup script, and you should be able to use them almost as they are, since the local imports will work in the package namespace.

What is the purpose of using `-m`? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
It seems to me python -m myscript and python myscript do the same thing: running a script.
What is the purpose of using -m? Thanks.
In some cases, especially for very small projects, python script.py and python -m script will be pretty much the same.
The biggest difference is when your module lives in a package and has relative imports. If you have a script that import something like from .module import some_name, you will most likely get a ModuleNotFoundError when you run it with python package/scripy.py. On the other hand, python -m package.script will produce whatever output you expected.
You can load modules and invoke them as script. The exact file name or path is not needed. Example:
python -mjson.tool myfile.json
This will print a formatted version of myfile.json, and it loads the module json.tool for this. Python searches for this module automatically. You don't need to know the exact path.

what is python runpy module? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I was wondering the purpose of runpy module in Python and how does it help in day to day development of the Python user community?
Can someone please explain the necessity, usage and advantages of runpy module in python?
The docs say:
The runpy module is used to locate and run Python modules without importing them first. Its main use is to implement the -m command line switch that allows scripts to be located using the Python module namespace rather than the filesystem.
You can run a python module like this:
python -m SimpleHTTPServer 8000
The runpy module enables this functionality. It may not be useful outside of this context.

What's the normal structure of a Python open source project and what's the preferred way of running the tests? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I wrote some code that I would like to share, and I would like to follow the best practices in creating/maintaining it's structure. I will host the code on BitBucket, and now I'm thinking about how I should organize it. Is this a good structure?
project_name/
lib/
test/
README
So, this will have the source in lib, and the tests in test. Is this how it's done in Python projects? This is the structure I saw was the most used with Ruby projects. Also, when I run the unit tests, is it considered good practice to do it like this:
set PYTHONPATH=`pwd`/lib
python test/a_test.py
The approach I've come to like is the following:
use distutils and create a setup.py file. (This is mostly useful
when you have lots of extension classes). This will allow you to install the
module system-wide or in a virtualenv directory.
If you want to have serious testing, but stay on the casual side of things,
doctest is what you want, because it can double as "bare-bones" documentation
(when you document the tests and include some commentary on what it's doing).
You can either use doctest to use tests in the docstrings of your code or
keep the tests in some separate .txt files.
You can integrate doctest by extending the setup command with an appropriate cmdclass=... entry in the setup.py file. See this example (CouchDB setup)
for one solution that integrates testing in setup.py. (It uses separate files that
have both the tests and the actual documentation, which is also a possibility).

Categories

Resources