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

Carl Meyer carl at oddbird.net
Fri May 8 08:45:23 PDT 2015


Hi Luke,

On 05/07/2015 07:03 PM, Luke Mergner wrote:
> 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?

Selenium is one obvious alternative, yes (and the one that I use). The
advantage is that there is a Python Selenium webdriver library
(https://pypi.python.org/pypi/selenium) so you can write your Selenium
tests in Python and avoid the "how do I run tests written in JS from
py.test" problem. You can use Selenium with PhantomJS if you like, but
you can also run your Selenium tests on real browsers (Firefox, Chrome,
etc).

If you do still want to run tests written in JS from Python, you can use
the pytest_collect_file hook to implement arbitrary transformation of
any type of file into a set of tests (this is what oejskit does). (For
instance, I have a pytest plugin for testing templates using
pytest_collect_file; the tests are written in YAML files and
transparently discovered and run by py.test).

Carl

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20150508/54d95aba/attachment.pgp>


More information about the testing-in-python mailing list