Test doubles for Python standard library HTTP classes - python

What widely-used Python library is there to make test doubles of the behaviour of low-level HTTP APIs from the Python standard library?
I have a code base (Python 2 code) which performs HTTP sessions using the various HTTP-level classes in the standard library: httplib.HTTPConnection and urllib2.HTTPBasicAuthHandler and so on.
Testing this code will be made much easier if I can create numerous test doubles with pre-determined behaviour and instrumentation for later inspection, to allow the functions I'm testing to interact with those doubles instead of the real thing.
Mocking the API is no problem, of course; the standard mock library makes that easy. What is needed here, though, is to pre-set specific responses at specific points during the HTTP session, without duplicating the rest of the low-level protocol implementation.
By “widely-known”, I'm hoping that the library will already be available in OS package repositories. Requiring a package from a third party would be a significant cost for this project, so it would help a lot if it's already packaged and in Debian.
What standard code libraries provide classes which make it easy to double the behaviour of httplib classes and urllib2 classes, etc. for test cases?

Related

How we can automate python client application which is used an an interface for user to call APIs

We have made an python client which is used as an interface for user. some function is defined in the client which internally calls the APIs and give output to users.
My requirement is to automate the python client - functions and validate the output.
Please suggest tools to use.
There are several ways to do that:
You can write multiple tests for your application as the test cases which are responsible to call your functions and get the result and validate them. It calls the "feature test". To do that, you can use the python "unittest" library and call the tests periodically.
If you have a web application you can use "selenium" to make automatic test flows. (Also you can run it in a docker container virtually)
The other solution is to write another python application to call your functions or send requests everywhere you want to get the specific data and validate them. (It's the same with the two other solutions with a different implementation)
The most straightforward way is using Python for this, the simplest solution would be a library like pytest. More comprehensive option would be something like Robot framework
Given you have jmeter in your tags I assume that at some point you will want to make a performance test, however it might be easier to use Locust for this as it's pure Python load testing framework.
If you still want to use JMeter it's possible to call Python programs using OS Process Sampler

Are there reactive state libraries like Mobx for Python?

I'm looking for reactive state libraries like Mobx for Python, i.e. on server-side rather than client-side of a web application.
Mobx is similar to classic reactive libraries like RxPY, but has a different focus: It is not so much avout the low-level event dispatching, but reacting on data changes, recalculating derived values (but only those affected, and being lazy on non-observed dependent values). And Mobx determines dependencies of calculated values automatically.
Also, the Vue framework has such functionality built-in, with an even better syntax, with the upside (as well as downside) by being closely tied to the framework.
Alas, both are JavaScript and targeted at client-side / user interface.
So my specific questions are:
Are there similar reactive state libraries for Python?
Do these provide integration for storing/observing data in files?
(This would essentially be an inotify-based build system, but more fine-grained and more flexible.)
Do these provide integration with relational databases?
(Yes, there is a conceptual gap to be bridged, and it probably works only as long a single server instance accesses the database. It would still be very useful for wide range of applications.)
Do these provide integration with webserver frameworks?
(i.e. received HTTP requests trigger state changed and reclaculations, some calculated values are JSON structures which are observed by the client through web sockets, long polling or messaging systems.)
I did one. It's called MoPyX. It's toolkit independent so you can just observe objects. But is geared towards UIs.
See: https://github.com/germaniumhq/mopyx
PySide2 demo: https://github.com/germaniumhq/mopyx-sample

Python Interprocess w/REST Interface options

Is there any tool/library for Python that will aid in interprocess communication, while keeping the API/client code easy to maintain?
I'm writing an application wherein I own both the server and the client portion, but I'd like to make it expandable to others via a REST interface (or something similarly accessible). Is my only options to write the boilerplate connective tissue for REST communication?
The REST interface should be implemented with small functions that call the actual Python API, which you will implement any way.
If you search here, on SO, the most frequent recommendation will be to use Flask to expose the REST interface.
There are libraries around that will try to turn the methods of a class into REST paths, and such, and those may save you a couple of hours on the onset, but cost you many hours down the road.
This morning I coded a backend service that way. The Requests calls to the external service are hidden by a module so the business logic doesn't know where the objects come from (ORM?), and the business logic produces objects that a simple Flask layer consumes to produce the JSON required by each matched URL.
#app.route("/api/users")
def users():
return json_response(
api.users(limit=request.args.get('limit', None)),
)
A one-liner.

functional testing during development

I am not exposed to many of the testing framework, and wonder any recommendation on achieving the following (functional testing) during development phase. Intention is to test a web application functionality (language agnostic?) though the exposed http (REST/JSON RPC) interface.
My backend in NOT written in Python, but because of the easiness of using requests library, and creating Ad hoc http request, I simply construct http POST/GET request with appropriate cookie, payload etc and check the response to validate the server correctness.
It is little tedious to enable specific test cases (comment out / boolean flag ), and verify the results. Any framework to make this more pleasant during the development phase where frequent changes are the norm.
thanks.
Well your on the right track with requests you could tie that directly into nose or unittest or any of the common python testing frameworks that exist, bit of background requests was actually written for testing flask
Use nose to run your tests. In this case, you can declare base classes of your tests to be like this:
class SlowTestBase(BaseTestCase):
slow = True
And run it like nosetests --attr="slow", or to exclude them --attr="!slow". You can find more on nose documentation at https://nose.readthedocs.org/en/latest/

Consume Python DAO from cocoa/objective c front end

My plan is to develop a multi-tier, multi-platform database application.
I would like to consume the data from cocoa/objective c apps, .net apps, and web browsers.
I don’t really know where to start and have been looking a Python, but can’t find if cocoa/objective c apps can consume python data objects.
Can anyone point me in the right direction as to how to achieve my goal?
My requirements are:
Data layer should be platform independent.
Whole system is scalable. Therefore multi tier.
Data access can be from cocoa, .net and web based clients.
You can make python and objective-c work together. Since you can use 100% normal C you can use the Python C interface. It's very tedious though.
There's also PyObjC. This acts as a bridge between Objective-C and Python. The documentation is pretty good and it will be much simpler than using the Python C interface directly.
You could also try using Thrift. Thrift is like Protocol Buffers by Google, but has support for generating Objective-C classes. You will have to write some boiler plate code to convert the data object into a thrift object; but after that is done you can pass information amongst any of the languages thrift supports. Documentation is on the thin side; I wrote a tutorial on using with Objective-C available on the thrift wiki here some time ago, not sure if it us up-to-date though as there have been several releases of thrift since then.

Categories