Continuous unit testing with Pydev (Python and Eclipse) Continuous unit testing with Pydev (Python and Eclipse) python python

Continuous unit testing with Pydev (Python and Eclipse)


This feature has been added to PyDev 2.0.1 with an option to relaunch the tests in the last test run whenever a python file change, with an additional option to rerun only the errors -- although it'll run the full test suite if no errors were found, as the idea is that you work through your errors and when all pass a final launch for the whole suite is done (then you can move on to another task).

The current nightly build has this feature incorporated.

Picture with new action


Pydev does have some unit-test integration, but that's only as a run configuration...so...

This is not a very elegant way, but if you:

  1. Enable Project->Build Automatically
  2. In your project properties, add a new builder of type Program
  3. Configure it to run your tests and select 'during auto builds'

Then at least you will get something that outputs the test results to the console on resource saves.


I just realized that PyDev has rather powerful scripting support. Unfortunately I don't have the time to do it all for you (but if you complete this, please post it here :)

If you create a file named pyedit_nose.py that looks like this in an otherwise empty folder :

assert cmd is not Noneassert editor is not Noneif cmd == 'onSave':    from java.lang import Runtime    from java.io import BufferedReader    from java.io import InputStreamReader    from org.eclipse.core.resources import ResourcesPlugin    from org.eclipse.core.resources import IMarker    from org.eclipse.core.resources import IResource    proc = Runtime.getRuntime().exec('ls -al')    extra_message = BufferedReader(InputStreamReader(proc.inputStream)).readLine()    r = ResourcesPlugin.getWorkspace().getRoot()    for marker in r.findMarkers(IMarker.PROBLEM, False, IResource.DEPTH_INFINITE):        if marker.getAttribute(IMarker.MESSAGE).startsWith("Some test failed!"):            marker.delete()    for rr in r.getProjects():        marker = rr.createMarker(IMarker.PROBLEM)        marker.setAttribute(IMarker.MESSAGE, "Some test failed! " + extra_message)        marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH)        marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR)

and set up Preferences->PyDev->Scripting Pydev to point to this directory you will get all projects in your workspace marked with an error every time a file is saved.

By executing a script that returns the test results in some easy to parse format rather than ls and parsing the output you should be able to put your markers in the right places.

See this for some starting points:

  • Jython Scripting in Pydev
  • IMarker is what represents a marker.
  • IResource is what you attach your markers to. Can be workspaces, projects, files, directories etc. resource.createMarker(IMarker.PROBLEM) creates a problem marker.
  • IProject is a type of IResource that represents a project. Use the members() method to get the contents.