[TIP] pytest: plugin for CasperJS or javascript integration tests

Jim Fulton jim at zope.com
Fri May 8 04:53:08 PDT 2015


On Thu, May 7, 2015 at 9:03 PM, Luke Mergner <lmergner at gmail.com> wrote:
> Hello,
>
> I searched the list archives, Stack Overflow, Github, and the internet
> broadly, but did not see my question answered. This makes me think that I am
> approaching my problem from the wrong angle.
>
> I have a small Flask project which serves a few single-page-applications in
> several javascript frameworks. My goal is to learn to write js front end web
> applications. It was much easier for me to build the server in Flask,
> Jinja2, and SQLAlchemy rather than use Node, which many of the js framework
> tutorials assume.
>
> On top of my unit tests written in python (pytest) and javascript (jasmine,
> jest, or karma), I also thought I’d write some integration tests with
> CasperJS or, later, to try PhantomCSS. The challenge here is that in order
> to automate the process I have to run a live server example (plenty of these
> around) while also running the CasperJS process. Since this is a learning
> project, none of this is necessary, but it helps me to learn how to do
> things “the right way.”
>
> Ideally, I’d like to run the CasperJS integration tests through pytest. I’ve
> spent some time sketching out a test runner in Python that collects the
> test_*.js files by name and runs them in a threaded subprocess. It catches
> the return code, etc., from which I can infer success or failure. (Cool! I
> wrote my first threaded module.) Then I thought, it sure would be nice to
> add this as a pytest-plugin, then the server can be started as a fixture and
> the tests added in the normal test discovery phase.
>
> There is actually a Github project that claims to do something like this for
> Django, but it doesn’t appear load or run the CasperJS files. The
> documentation for pytest allows for adding tests through the
> pytest_pycollect_additem hook. However, the functions assume an inspectable
> python object. Option one is to just add a naive python wrapper around the
> javascript tests and see what happens.
>
> Option two would be to wrap the subprocess call in a way that I can return a
> subclass of _pytest.python.Function or to create a subclass of
> _pytest.main.FSCollection which acts like the PyCollection? I’m wondering if
> I can write subclasses of the Collection/Session and Function classes that I
> run along with the PyCollection lifecycle. It seems like to do this right,
> you’d want to give pytest an Item or Node class that mimics what it expects
> from introspecting python code. This at least is what I think after looking
> at pytest source.
>
> Is this worth pursuing? Is there an obvious alternative, like Selenium? What
> can anyone recommend in terms of implementation?
>
> I appreciate your insight and answers.

I really like running my WSGI apps, in-process using threads, in my
Python test runners.  This lets me set up tests by manipulating server
state and perform many assertions by looking at server state.

See https://github.com/zc/wsgidriver, which provides a little help
managing a WSGI application inside a test runner and does some
WebDriver setup.  I use this model for functional testing web apps as
well as native mobile apps (using tools like Appium and Selendroid).

Jim

-- 
Jim Fulton
http://www.linkedin.com/in/jimfulton



More information about the testing-in-python mailing list