Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyDev Unit Test Set Run Directory

Tags:

eclipse

pydev

I have a PyDev unit test module that lives at the path:

$(PYDEV_PROJECT_ROOT)/tests/my_unit_test.py

I am attempting to use Eclipse PyDev's unit testing facilities. My unit test must read a configuration file like so:

(foo,bar,baz) = myModule.readOptimizationConfig("tests/optimization_config_file.cfg")

However, this will not work because PyDev goes into the 'tests' directory before running, and so specifying 'tests/' in the path given to readOptimizationConfig makes it attempt to load

$(PYDEV_PROJECT_ROOT)/tests/tests/optimization_config_file.cfg

However, I also need to run these tests using nosetests from the command lin. This is because, in order to run ALL the tests for my project, rather than the option for running them in a particular file that is provided by default, the easiest solution was to just use the 'nosetests' command, rather than messing with Eclipse launch configurations. However, nosetests needs to be be run from the $(PYDEV_PROJECT_ROOT) root directory, so it needs the 'tests/' specified in the path.

Is there a way to force eclipse to run the unit tests from the project root directory, so that the paths that I pass to readOptimizationConfig will work for both methods?

like image 259
Sam Manzer Avatar asked Nov 24 '25 17:11

Sam Manzer


1 Answers

It is possible to do this in PyDev, but you have to do it per every launch configuration so it's a bit boring.

Anyway, you first try to run as your script containing the unit-test (as you normally would - e.g. the dropdown menu next to the green "Run" arrow button then Run as... - Python unit-test). This launch will fail because of your missing cfg file. Now go to Run configurations (Run dropdown - Run configurations), open the Arguments tab and in the bottom under Working directory enter the path you want (or browse for it using the Workspace... button). For example if you want to run from project root and your project is called awesome-project, you would write:

${workspace_loc:awesome-project}

Now you should have a valid launch configuration that you can use from both the Run and Debug menus. I sometimes rename these configurations to something noticeable right away e.g. "awesome-project TEST".

like image 110
metakermit Avatar answered Nov 27 '25 15:11

metakermit