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?
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".
Related
I have a workspace with 7 different Dart / Flutter projects. Currently, to choose which project to run, I select a file from that project and then f5. Using this approach seems to be quite error prone as I sometimes have the wrong file open and it loads the wrong project. Other times I it takes a few seconds to find a file and open it to run.
It would be nice if there was a selector for me to choose which project or a way to select a default project to run regardless of which file I have open. Is this possible at all?
You can control this by creating a Launch Configuration (see https://code.visualstudio.com/Docs/editor/debugging#_launch-configurations) file (launch.json). You can create this by clicking the Cog icon on the Debug side bar. It'll be created at .vscode/launch.json.
You can set the cwd or program fields in the config to relative paths from the folder you've opened to control what launches. program lets you specify a specific script, whereas cwd lets you specify a project root (where the Dart plugin will try to guess the best entry point, like bin/main.dart for Dart, or lib/main.dart for Flutter).
Another option is to use VS Code's "Multi-Root Workspaces", where you'll be able to select which workspace folder to debug from the debug side bar, however this generally results in saving a .code-workspace file that some users (including myself) find an annoyance.
I have a question about eclipse and make. I have created a make environment which acts as follow:
I have a batch file (Start.bat) which SET´s variables for further usage (e.g. SET TOOL_PATH = blabla).
After calling this I use my make.bat <option>, which calls the make.ext -f Makefile.mak <option> with the given option e.g. make.bat all -sj4. The environment variables which are set in Start.bat are very important for the running of the overall make environment.
This works very fine and now I would like to import this in eclipse but I am not familiar with eclipse. I need from eclipse that when I would like to run a make, first the Start.bat is called and then the make.bat <option> is called.
What I need to do?
You can import your project as 'existing make file project'
In the project settings you should be able to define pre-biuld steps and call the batch there or define a specific target (e.g. start) that calls the start batch and is executed before your 'make'.
I dont know exactly at the moment how to do this the best way, but I think you are fast into it.
I have installed Erlide in Eclipse, and trying to create an application. The Erlang project is named demo. It contains three folders - ebin, include and src. The demo.erl file is in src, for simply printing Hello World.
To run it in Eclipse shell, I choose Run from the top menu, and click Run as Erlang Application. Then a console opens in the sidebar. There I type c(demo) and press CTRL-Enter. Then I get an error message like demo.erl:none: no such file or directory.
Then I do pwd(), and it shows C:/Users/myName/Desktop/eclipse-jee-juno-SR1-win32-x86_64/eclipse even though the Erlang project is in workspace C:/Users/myName/Desktop/workspace/Erlang. Is there any way make Erlide run all Erlang applications from their directory directly?
If you have set Project -> Build Automatically in Eclipse to true, you don't need to type c(demo). explicitly to compile the demo module, it's done automatically when you save your file. But if you have not set it, you'll get exception error: undefined function if you try to use some of the functions in the module for example.
To make the c(demo) command work properly and find your .erl files, there are several ways to set the current working directory:
Use Run -> Run Configuration -> Erlang Application -> Runtimes -> Working directory -> enter the full path to the /src folder of your project (without quotes).
Then hit Apply and restart Eclipse (File -> Restart). If you try to Run the project without first restarting the IDE, your changes will not take effect i.e. the pwd(). command will still be returning the old working directory path.
You can set the working directory in the erlide console with the command c:cd(the/full/path/to/your/src/folder)., before the call to c(demo). The path should be put in quotes.
You can also set the working directory in the .erlang file by typing there the same command c:cd(the/full/path/to/your/src/folder). (the path again should be put in quotes). The file must be placed in you user directory, for Windows this would be C:/Documents and Settings/YourUsername. It's the same directory were also .erlang.cookie and erlide-debug.txt are placed. Windows Explorer will not allow you to create a file named .erlang, so you have to use the command copy NUL .erlang in Command Prompt (cmd.exe) for this. Then you can edit it with any txt redactor. Don't forget to restart Eclipse again after it.
Also note the following:
You can use only / and not \ in the path (even in Windows).
If you have entered a wrong path (with \ or to a non existing directory), it will be ignored and the working directory will not be changed at all.
After changing the working directory to your/src folder, your beam files will start to appear there too instead of the /ebin folder.
If you set a different path in both .erlang file and Run -> Run Configuration -> Erlang Application -> Runtimes -> Working directory, pwd(). will return the path in .erlang file.
You don't need to run c(demo). The beam code is loaded and reloaded automatically, whenever the source is changed and saved.
If you still need to set a working directory, go to run->run configuration and you can edit the configuration.
/Vlad
I set up unit testing in Zend Studio last week, and it was working fine.. until suddenly after some refactoring, I got an error that the following file was not found in ZendPHPUnit.php:
/var/folders/Td/Tdnh++2KEdWAsk8Y0O4N0k+++TI/-Tmp-/zend.phpunit.UserMapperTest.php.2428213892936827201.php
The file path is stored in $_SERVER['ZEND_PHPUNIT_TESTS_LOCATION'] in ZendPHPUnit.php
I checked the folder and I found
zend.phpunit.UserrMapperTest.php.6031927106542896607.php (the number is different)
I was a little desperate so I made it work by forcing
$_SERVER['ZEND_PHPUNIT_TESTS_LOCATION'] = '/var/folders/Td/Tdnh++2KEdWAsk8Y0O4N0k+++TI/-Tmp-/zend.phpunit.UserMapperTest.php.6031927106542896607.php';
Eventually, after I worked with a few other test cases, the problem fixed itself. Now, I refactored some code again, and the problem is back. None of my testcases work.
Restarting the comp doesnt help, Project -> Clean doesnt help. I am on a mac running Snow Leopard.
Any insights on what is causing this?
Thanks!
The problem is that PHPUnit doesn't work with the files themselves but with copies of the files that it creates on the fly and stores on directories that are also created on the fly. These files are modified to include PHPUnit logic. This goes for the test files themselves as well as for the php.ini (which forces you to load extensions in the main php.ini file for the tests, since additional .ini files are ignored). Every time you run the tests these files will be re-created with a new unique name (original name plus unique identifier).
The best way that I've found to work around this issue is to create launch configurations for the tests and save them as .launch files in your project (right click -> run as -> create new PHPUnit config -> select "shared file" in the common tab). Once you have the launch configs you can just run this by opening on the editor and clicking the Run button. You will see that after every time you run them there will be a line like the following on each launch file that has changed (easy to see if the configs are in version control):
<mapEntry key="ZEND_PHPUNIT_TESTS_LOCATION" value="/var/folders/my/ph9spb0s45z5_11l9tqw256r0000gn/T/DefaultWorkspace.phpunit.AssignmentRequestControllerDateCriteriaTest.php.4951739174960507380.php"/>
I usually just commit this change together with whatever other changes I've been working on at that time.
It's still kind of annoying that the file changes and thus the launch config, but at least in this way your tests will always run and you don't have to worry about having to re-run the manually, or clean the project, or anything like that.
I want to run my working pydev project python code by double clicking the main module (outside of eclipse): xxx.py
The problem is that due to my imports being in different packages:
from src.apackage.amodule import obj
when xxx.py is double clicked it complains it doesn't know where the imports are (even though when I run xxx.py in pydev it magically knows what I'm importing).
A simple workaround is to remove all of the packages and move all of the modules into one directory (that obviously works but is very inconvenient)
How can I run my code in the file system without doing that work around?
This page answers my question excellently:
http://blog.habnab.it/blog/2013/07/21/python-packages-and-you/
Bottom line is always execute your code from the top, highest level, root directory (e.g. using a minimal main.py file that executes the main script of your program). Then, use absolute imports always and you never have a missing module issue since you start the program from the top directory and all imports are based off that 'home' path.
The problem you encountered is the natural behavior of most languages. A programm only knows about its working path (the path it is started in), the paths which are registered in the environment variables and at least relative paths.
The "magic" of the executable you created is therefore: It collects all scripts/modules needed, and copies/combines them next to/in the executable. The Executable then runs within the directory where all other scripts also reside and voila ...
If you are not happy with your workaround of creating an executable every time you want to run your project without PyDev there are two alternatives.
First but not the one I would suggest is registering the working path into in the environment variables.
Second and the one I think is much better: Create a link to the python executable and alter the calling string of the textfield "Target:". Append the path to your script you would like to run. Then alter the textfield "Start in:" and enter the project directory. After you did this you should be able to start your project with a simple double click.
(If you rely on external libraries which are neither on the path nor in you project you could search for appending paths temporarily to the pythonpath via the sys module.)
I hope I could help a bit.