When I run Gradle tasks like test from IntelliJ, I set VM options and/or environment variables for all tests in "Edit configuration" dialog and they work. See my question at how to set spring.config.location for a gradle spring boot project in Intellij IDEA community version?
Now, if I want to run a single test, I must:
run test, which fails of course
edit configuration to add VM options and/or env vars
run it again
Is there some way to configure the VM options and/or env vars in build.gradle so that would be picked up by every single test automatically?
PS: the args are -Dspring.profiles.active=... and -Dspring.config.additional-location.
Try setting the system properties in the file gradle.properties like so:
systemProp.spring.profiles.active=profileName
spring.config.additional-location=additionalLocation
See also: https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_system_properties
Thanks #tarts, but I think I have found one thing maybe better: setting a template of run configuration in IDEA. The question may be reworded into: "can I in some way, not limited in build.gradle, to set these variables?"
In "Run Configuration", on the left side, there is a category named "Template" listing all possible categories of tests we can run and providing template for each, where we set universal values for single tests.
You can see that "Gradle" and "JUnit" and other many categories are listed, so we can change VM options here, then all unit tests are run with this param set.
Related
I'm using IntelliJ IDEA to write and run e2e tests with JUnit. My tests require some environment variables to be set, so I set these in the Run Configurations dialog (through Run-->Edit Configurations, Environment Variables field).
This works fine, but the problem is whenever I run a single test (e.g. by clicking the green "play" icon to the left of the code editor), it creates a new run configuration for me to run this single test without the environment variables I defined, which obviously causes it to fail. I need to open the Run Configurations dialog again and copy the environment variables from another configuration to the new one. Note that I need to do it for every single test that I run, which is very annoying.
Is there a way to set the environment variables only once and use them for every test that I run?
You can expad the "Defaults" in the list to the left, pick JUnit and then set the env variables there. This config will then get copied to whatever new configurations are created afterwards:
As it is specified everywhere for executing robot scripts you have to create maven project.I created the maven project and have added all the dependencies and plugin required for the execution. But when i create the maven project then by default a class ic created as AppTest.java in my src/test folder.Now when i add this .robot file in my test folder or main folder.It is not getting executed.Moreover i have a confusion how to execute these scripts.Please help me with this.Where should i place these file so that they could be executed.
I believe that at this time (7 months after your question) you've found a solution for the problem. If yes, I suggest you share it with the community.
Here's my solution, assuming that you're using Google Chrome:
Open the Edit Configurations window for your maven project, select the tab "Parameters", and in the field "Command Line" type "robotframework:run". It should look like this:
Then you select the tab "Runner" and add the property "webdriver.chrome.driver=/path-to-the-webdriver". It shoud look like this:
(note: uncheck the "Skip tests" check box. Mine is checked on the screenshot because I was testing other things.)
What's a little frustrating is that we can be misled by the error messages which say that you have to set the environment variable "webdriver.chrome.driver". In this case, we're tempted to add this variable to the field "Environment variables".
However, IntelliJ creates its own environment on run. That's why the field "Environment variables" doesn't work. When you add that property to the "Properties" box, the IntelliJ will convert it into a parameter in the run command, which will create the proper environment for the project.
Something like this:
/usr/lib/jvm/java-8-oracle/bin/java
-Dmaven.multiModuleProjectDirectory=/home/acampos/study/robotframework01
-Dmaven.home=/home/acampos/programs/idea-IU-172.4343.14/plugins/maven/lib/maven3
-Dclassworlds.conf=/home/acampos/programs/idea-IU-172.4343.14/plugins/maven/lib/maven3/bin/m2.conf
-javaagent:/home/acampos/programs/idea-IU-172.4343.14/lib/idea_rt.jar=36889:/home/acampos/programs/idea-IU-172.4343.14/bin
-Dfile.encoding=UTF-8
-classpath /home/acampos/programs/idea-IU-172.4343.14/plugins/maven/lib/maven3/boot/plexus-classworlds-2.5.2.jar org.codehaus.classworlds.Launcher
-Didea.version=2017.2.5
HERE!!!
-Dwebdriver.chrome.driver=/home/acampos/programs/chromedriver/chromedriver robotframework:run
So, when you run your maven project it will run the goal that's described on the Parameters tab, Command line field.
I hope it helps. And, again, if you've found another solution, please share with us.
Good luck!
every now and then I am launching JUnit tests from within Eclipse by using
Run As > JUnit Plug-in Test
By default Eclipse assumes you are running something which requires a workbench and chooses in the "Main" section of the launch configuration
LaunchConfig > Main > Program To Run > Run an application >
org.eclipse.ui.ide.workbench
I can understand why this is the default, but for me (and for all in our team) this is never ever the case. We always need to run our JUnit Plug-in Tests as
LaunchConfig > Main > Program To Run > Run an application >
[No Application] Headless Mode
How do I change this default behaviour?
I am currently using Eclipse 4.4.
It seems a custom LaunchConfiguration-Extension is a viable solution attempt.
What I did was to create a new, custom LaunchConfiguration-Extension which is 99.999% build on the JUnitLaunchConfiguration. I only had to add a custom
BlaBlaJUnitPluginTestLauncher extends launching.JUnitLaunchConfigurationDelegate
which overrides the
launch(ILaunchConfiguration, String, ILaunch, IProgressMonitor)
method to adjust the application parameters according to our needs.
BlaBlaJUnitPluginTestTabGroup extends org.eclipse.pde.ui.launcher.JUnitTabGroup
To be able to initialize the LaunchConfig dialog with the default parameter, I had to:
Add a custom BlaBlaPluginJUnitMainTab extends PluginJUnitMainTab
Create a custom JUnitProgramBlock implementation (BlaBlaJUnitProgramBlock)
Creating an instance of BlaBlaJUnitProgramBlock in the BlaBlaJUnitPluginTestTabGroup.BlaBlaPluginJUnitMainTab.createProgramBlock() method
Overriding setDefaults-method (not sure if its really neccessary) in BlaBlaJUnitProgramBlock
Overriding initializeForm-method in the BlaBlaJUnitProgramBlock and adjust parameter there too.
Leading to the following result:
This answer is a near miss:
Try this:
Manually create and configure one "good" launch configuration.
Next time you want to launch a test that doesn't yet have a good launch configuration:
Select the file and invoke Run As > Run Configurations ... (i.e., don't yet select JUnit Plug-in Test!)
In that dialog select a good launch configuration of the same kind, and ...
Then click New Launch Configuration (upper-left corner)
Now the newly created configuration should "inherit" the configured values from the the good configuration.
Truth is:
You can duplicate an existing launch configuration (leaving you to manually select the test to launch)
The Debug team once had plans to support launch configuration templates.
Edit 2018:
Since Eclipse Photon, the Java debugger supports launch configuration prototypes. I just filed Bug 536728 to request this also for test launches. Feel free to chime in (or contribute) on that bug.
if u r looking for only shortcut for convenience then eclipse remembers last execution. After using run as and saving ur run config, just use "Run as" button in toolbar.
besides this eclipse comes with flavour for testers, u can check that out.
Also since you are talking about unit testing see if you can make use of ant build or even better converting to maven based project. Maven has integrated support for testing.
There seems to be a simple and effective heuristic in place, which decides whether or not a JUnit Plug-in Test should be run headlessly or with an application:
Make sure that the plug-in containing your tests has no dependencies on anything org.eclipse.ui.
Without that dependency [No Application - Headless Mode] is selected by default for newly created launch configurations.
With that dependency the default is Run a product, with s.t. like org.eclipse.platform.ide preselected.
Is there a way to set System Properties which are used for every JVM that is started by NetBeans (NB 7.3.1 on Win 7)?
In all my maven projects I use Log4j wich needs a log4j.properties file, to where I want to point to by a System Property -Dlog4j.configuration=file:/c:/log4j/log4j.properties
I could set Global Execution Options for Maven: Tools > Options > Java > Maven > Global Execution Options. But then when I run a particular JUnit test class in NetBeans (thus without Maven), NetBeans won't use these JVM System Properties set in the Global Execution Options field.
Same issue is when a Main class in a Maven module is run in NetBeans. To set JVM System Properties there are Project Properties which could be set: 'Main Class' and 'VM Options'. But these Project Properties are only used by NetBeans when the 'Run' command is used from the context menu of the maven module. (Unfortunaly this menu item doesn't have a shortcut key (normaly Shift-F6)).
I have worked around this by setting a System Variable JAVA_TOOL_OPTIONS=-Dlog4j.configuration=file:/c:/log4j/log4j.properties.
Edit
A question was asked for the same problem I ran in to: How to make Netbeans use specific JVM parameters when running tests? In my case the problem was caused by the option Compile on Save which I had swiched on (File > Project Properties > Build > Compile > Compile On Save). In that case it seems that NetBeans doesn't use Maven. For further info about CoS: http://wiki.netbeans.org/FaqCompileOnSave
There's a file called project.properties in the nbproject folder. In this file there is a lot of configurations you can do that are not in the GUI menus. I don't know exactly what to put there to achieve what you want, but I found this on one of my NetBeans projects config:
# Space-separated list of JVM arguments used when running the project
# (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value
# or test-sys-prop.name=value to set system properties for unit tests):
run.jvmargs=
run.test.classpath=\
${javac.test.classpath}:\
${build.test.classes.dir}
I'm writing a Java library with a lot of jni code. Pretty much every test case needs to load my jni dll, and I have a lot of test cases. In order to run the test cases out of Eclipse's Junit launcher, I have to create a run/debug configuration and edit the VM arguments and environment variables.
I would like a way to set the VM arguments and environment variables to a default for the entire project and have new run configurations include the default entries. From what I can tell, Execution Environments maybe do something like this but I seem to need the PDE to get them to work(?)
Specifically, I want to enable assertions on my project by default and include the path to my native dll in the PATH environment variable. I can't use the "Default VM Arguments" setting in the JRE definition panel because my dll depends on a number of others and java.library.path isn't used for dependency resolution, PATH is. Is there a way to make Eclipse do what I want?
So, here's what I did.
First, my specific problem was that I have a lot of run configurations, I create new ones on the fly, and I needed certain system properties set for unit tests. Setting them under the 'args' tab of run configurations was undesirable for my workflow. Also, I wanted the same command-line args set for all of my tests. I also don't run my app from inside eclipse. It's a dev-environment only.
So my solution was to add it to the command-line of my JRE. Preferences -> Java -> Installed JREs. Clicking edit gives you a window where you can specify default VM args. I just set the system properties I need for testing there.
Hope this helps.
How long does it take to run all of your tests for the project?
If the answer is Not long then create a project-wide JUnit launcher. If occasionally you would need to do a run on a single test case ( in order to debug or something ), you can copy all your settings from the project's junit launcher. I think you can even clone your project launcher to run a specific test case.
Run->Run Configurations...
Create new JUnit launcher.
On 'Test' tab select Run all tests
in selected {...}
Connfigure JVM options, classpath,
environment etc. for this launcher
Optional, but highly recommended. On
Common tab -> Save as -> Shared
file, and check-in launcher with
your project
One more thing I would do is to define a system property in launcher VM arguments, check for this property in #Before function and throw exception if the property is not set. This way you will know that your test fails because it is not using the right launcher.
If I understand your question correctly, I think Alexander is on to the idea with cloning the project launcher. Eclipse lets you duplicate launch configurations with a single click - simply setup one configuration with the parameters you require and click the button in the top left to duplicate it whenever you create a new one.