views:

2053

answers:

3

Is there a way to integrate background unit tests with the Pydev Eclipse environment?

My unit tests run well, but I would like to integrate them to run in the background based on source file changes (e.g. with nose) and to integrate the result back to Eclipse (I'm thinking big red X when tests fail with a console and trace log view).

No, a command prompt running nose on the side does not count.

I have had this Eclipse integration when developing RoR stuff.

Thanks,

Tal.

+4  A: 

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.

Henrik Gustafsson
+1 worked for me
mlsteeves
+3  A: 

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 None
assert editor is not None

if 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.
Henrik Gustafsson
A: 

I am using Nosy which is handy.

leo
Link was down for me. Archive.org to the rescue: http://web.archive.org/web/20080610082506/http://jeffwinkler.net/2006/04/27/keeping-your-nose-green/
pydave