Intellij Idea : view test coverage on a maven project - maven

Is there a way to run all test in an root pom and collect test coverage in Intellij Idea ?

Create a Run Configuration that will look for tests in the whole project. Make sure that you choose All in package and In whole project in the configurations dialog:
Make sure that you choose the <default> package in the Choose Package dialog.
Now you can select to run this configuration using Run 'All in project' with Coverage:
The result will be presented in a separate coverate window:
And you will also see the result in the Project View window for a fast overview:

Right click on the parent module (root pom), and select Run 'All Tests' with Coverage.

There is a 'Coverage' plugin, which may not be enabled by default.
Find it at File->Settings->Plugins.
This will enable the 'Run with Coverage' buttons and menu items.
A short official overview video is here: Code Coverage by IntelliJ IDEA

Related

How to get the Maven project window in Intellij 14 | Update: cannot see the right side "tab bar" with Maven project

I have a very simple question, but I Googled and somehow I couldn't find it. I was wondering how can I get the
in Intellij 14 or something similar?
I saw it in a tutorial, but couldn't find the panel. In my case when I followed this video exactly, I couldn't see the panel and I couldn't find it anywhere. I have IntelliJ 14.
UPDATE
I just experimented a bit with the Intellij interface but I can't seem to see the right hand side bar. See
I had similar problem until I right-clicked the option in POM "Add as Maven Project".
Notice: IDEA 2019.2 and I couldn't even find Maven in "Tool Windows" of the (existing) project before it.
First, make sure you have enabled maven plugin in
File → Settings → Plugins → Maven Integration
If so and still the tool window is hidden then
go to View → Tool Windows → Maven Projects to open it.
For those stuck with this issue, enable View -> Tool Buttons.
Finally found the solution, right-click on the pom.xml file (parent pom.xml if it is a multi-module project) and hit "add as a maven project".
You can find the Maven Projects pane by going to Help > Find Action or press Ctrl + Shift + A and type in "Maven Projects". However I can't see it anywhere in the elaborate menu's.
I had the same problem with a maven project, the tab wasn't showing up and even in the View->Tool windows menu, there was no maven item.
After searching the internet i could not find a solution but then i looked in Intellij idea
and noticed a Even Log showing a number in a red circle, i clicked it and then read an error message:
Non-managed pom.xml file found:
Add as Maven Project
check out solution proposed by invzbl3, It might help.
Simple solution to see maven on intelliJ is go to your right click on your pom.xml and then click on Add as Maven project.
Very simple. NO PLUGINS.
Maven comes by default so you should press two times the shift key and write "maven".
Then import the maven project ".pom" and the bar cames automatically after selecting the pom.
The question asked is how to show the sidebar, not show the Maven Plugin. On my version of Intellij this is enabled with the following:
View -> Appearance -> Tool Window Bars
if you have a pom file in your project, right click the file, click "add as maven project"
This got it back for me. None of the other solutions worked. I had no warnings for unmanged poms, toggling toolbars did not help and the maven option just where it should be anywhere!
It's a bit late, but I had the same problem with missing sidebars. At least in IDEA 2017.3 there is an icon on the bottom left of the screen that toggles the sidebars. That's how I got it back.
Right-click on your project > Add Framework Support > Maven
If View → Tool Windows → Maven Projects doesnt work that means tools button is deselected
go to View -> Tool Buttons and select it . You should be able to Maven window now
Try with File-->Project structure --> Modules-> Remove Parent/existing modules and add parent/all modules again
None above helped.
In my case I had to ad Java SDK. It keeps disappearing for some reason.
Right click on project folder -> Open Module settings -> Project in this window define SDK under SDK:
Maven toolbar appeared after doing that.
Weird but this works at times:
Disable File → Settings → Plugins → Maven Integration first and restart Intellij. Then re-enable it back and restart - this might fix the issue.
Enable maven plugin first in
File → Settings → Plugins → Maven
Also, make sure you enable Tool Buttons in
View → Tool Buttons
Check if at the bottom line of Intellij, it says "non-managed pom files found."
You can then "Add as a Maven Project"
Make sure your Maven plugin is enabled as well. In my case, it was disabled for an unknown reason. To enable it: Settings → Plugins → Installed (Scroll down to Bundled section)
If you not use Maven, can delete it.
open ProjectName.iml file in root of project of module.
then remove "org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true"" from file.
save and reBuild project.
Looks like my maven integration plugin got disabled.
Go to IntelliJ Idea --> Preferences
and enable Maven integration plugin.
Looks like Intellij Idea disables dependent plugin also, when one of the plugins are disabled.
Make sure that the home directory of the project is correct. I was creating Intellij project one level above the actual maven project folder and that's why the maven tool window wasn't showing.
No Need to do any settings i have attached the image of intelij in that marked area click on that multi window suport then you will be able to access the panels for all

Invoking Actions other than Build and Clean & Build

I am new to NetBeans and am trying to define and run various custom goals for my Maven proj. My Maven proj uses a number of plugins; once for building the Java classes, another for pre-compiling JSPs, another for building an OSGi bundle, and another for deploying to my dev app server.
It seems that by default, NetBeans has Build, and Clean & Build as easily accessible "build options" in the main toolbar bar. These trigger the install goal which runs through the entire build processes (compiles everything and deploys to my dev).
In NetBeans, I can edit the Properties for the project, and define a "development" profile, and then define custom actions (or modify the default NetBeans actions).
How do I:
Invoke Actions that aren't Build or Clean & Build in NetBeans
Define only certain goals for specific plugins to run, and invokes those actions.
First go to the Project Properties > Actions and create your MVN build
The above will run:
mvn install -P initdb,initdb-qa -DskipTest=true -Dprop.name=dev
Then it will become available under "Custom"
This is really just a comment on #Ev0oD's answer, but I want to add an image so it has to be a separate answer.
After I created the toolbar button using:
Tools -> Options -> Java -> Maven -> Execution -> Edit Global Custom Goal Definitions
I then had to add the button to the toolbar using:
View -> Toolbars -> Customize (or just right click the toolbar header and select Customize)
I could then see the new button in the "Maven" group in this dialog:
I then had to drag the button onto a toolbar as shown. Note: Don't try to drag it to the blank part on the right of the toolbar header as this won't work.
If you want to use some custom goals on more projects and you want to have a quick access to them, you can use global custom goals, that are present in NetBeans 8.0.1 (not sure in which version they were introduced).
Access Tools -> Options -> Java -> Maven -> Execution -> Edit Global Custom Goal Definitions...
There you can add many of these and they will be present for all projects in the Custom section, as shown in the accepted answer.
A good thing is you can add it also to the Toolbar by clicking (Show in toolbar) and selecting one of six different colors of icon available. You will end up with something like this:
alternatively you can use the Custom popup on project node and within the Custom... action's dialog, setup goals for execution and eventually remember the setup for subsequent executions. The remembered item will appear in the same popup menu. For global (for every project) goal definitions, please see the global options.

How do I automatically perform unit tests on each build?

How do I automatically perform unit tests on each build?
I tried to add the Unit Tests target to the Project Target as a dependency, but that doesn't seem to run the actual tests.
Set the Test After Build build setting to Yes. Choose Product > Build For > Build For Testing to build the project and run the tests.
Xcode 5 Update
Xcode 5 does not support the Test After Build build setting. I don't know of any way to automatically run tests on each build in Xcode 5. From the OS X menu bar choose Product > Test or press Command + u to run unit tests in Xcode 5.
To establish the relationship between your targets:
In the Scheme picker, select "Edit Scheme…"
Select the Test phase
Make sure you're seeing the Info tab, not the Arguments tab
Click '+' and specify your testing target
You should see a list of tests. Click 'OK'
Then to run the tests, choose Product > Test, or simply ⌘U from the keyboard. This will:
Build your main target
Build your test target
Execute your main target according to its type, running the tests
Here's what I had to do to get Xcode 4 to automatically run unit tests on build.
Enable "Test After Build" for your main target. (See Mark Szymczyk's answer)
Enable your Test target to run during the build phase of your main target's build scheme.
Click Product > Manage Schemes menu item
Double click your target's scheme (I only had one in my list)
Unfold the Build accordion on the left
Click the Build option in the accordion
You should see you test project on the right, make sure the Run checkbox is clicked.
I thought I'd give a high-level explanation of the steps that worked for me. (OS X 10.7.4, Xcode 4.3.3) If you are unfamiliar with things like Build Phases and Schemes, here's a great resource from Apple: http://developer.apple.com/library/ios/#documentation/ToolsLanguages/Conceptual/Xcode4UserGuide/Building/Building.html
For a new project, make sure you check the "Include Unit Tests" checkbox on the page where you enter the name of the new project. Click on the project icon in the Project Navigator to see the two targets that have been created automatically. They'll be called something like "MyCocoaApp" and "MyCocoaAppTests". Select the MyCocoaAppTests target (NOT the MyCocoaApp one!) and select "Build Settings" from the choices at the top of the view. Enter "test after build" in the search bar near the top of the view. You should see a single setting for "Test After Build" and it's set to NO by default. Click on the NO setting and change it to YES. One more step. Bring up the scheme for the project. (Product Menu -> Edit Scheme...) Select "Build" in the column on the left. You should see your two targets and some check boxes to the right. On the line for "MyCocoaAppTests", check the "Run" checkbox and hit "OK". You're done. Type Command-B. Your app will build, the unit tests will build, the unit tests will run and fail on an error that is there by default.
If you have to add unit testing to an existing project, it's a bit more involved. First, follow these instructions by Apple: https://developer.apple.com/library/mac/#documentation/developertools/Conceptual/UnitTesting/02-Setting_Up_Unit_Tests_in_a_Project/setting_up.html#//apple_ref/doc/uid/TP40002143-CH3-SW1
If you don't miss any steps, you should now have unit tests set up that run when you select (Product Menu -> Test). Now, do the two steps described above: 1) Set the "Test After Build" setting to YES for the MyCocoaAppTest target; 2) Check the "Run" checkbox for the MyCocoaAppTest target in the Build section of the Scheme for the app. LAST STEP: You have to add a new "Run Script" build phase to the MyCocoaAppTest target (NOT the MyCocoaApp one!). Select the test target, click on "Build Phases", click on the "Add Build Phase" icon in the lower right, select "Add Run Script". A "Run Script" section will open up below the other build phases. The script field has this text in it: "Type a script or drag a script file from your workspace". In that field, enter: "${SYSTEM_DEVELOPER_DIR}/Tools/RunUnitTests". (Including the quotation marks.)
That's it. Hit Command-B. Your app should build. Your tests should build and then run. Good luck. Phew.

Cannot find any coverage data (ASP.NET MVC2)

I am having some issues with getting Code Coverage working with an out-of-the-box ASP.NET MVC2 Web App
VS2010 Ultimate, File > New Project > ASP.NET MVC 2 Web Application > Yes, Create unit test project with Visual Studio Unit Test. I then Rebuild All, Run All Unit Tests, Go to Code Coverage and get the following message:
Cannot find any coverage data (.coverage or .coveragexml) files. Check test run details for possible errors.
All the unit tests passed. (And I haven't touched a line of code yet)
I did find the following on the web:
http://www.vbforums.com/showthread.php?t=615377
which says to do the following:
Test -> Edit Test Settings -> Local
In the test settings dialog, click
"Data and Diagnostics" Ensure "Code
Coverage" are checked, and
double-click on it Check of the dll's
you want code coverage enabled for.
But, when I go to Test > Edit Test Seetings, all I see is the grayed out menu item stating "No Test Settings Available".
Any ideas?
Edit: slowly gaining traction. See: How to create the vsmdi/testrunconfig file when importing a Visual Studio test project?
I have had this same problem occur when I added a test project from another source (ie added to, but not created in the current solution). When doing this, local.testsettings, Solution.vsmdi, and TraceAndTestImpact.testsettings are NOT created inside your solution.
This fix is really quite simple, though. Simply right click on your solution and click Add -> New item. A new window will appear. On the left-hand side under General and Performance should be Test Settings. This should let you add a .testsettings file that you can now edit.
More information can be found here:
http://msdn.microsoft.com/en-us/library/ee256991%28v=vs.100%29.aspx

VS2008: Unit Testing with Code Coverage doesn't work with /CLR

I'm trying to set up unit testing with code coverage in VS2008, for a C++/CLI DLL which is compiled with /clr (not /clr:safe or /clr:pure - it has to be /clr because it uses MFC).
The unit tests work perfectly but the coverage information only works if I compile with /clr:safe or /clr:pure. For /clr the Code Coverage Results window shows the following message:
Empty results generated: none of the
instrumented binary was used. Look at
test run details for any
instrumentation problems.
I've also tried "going offroad" but when I load the coverage file into VS, it also contains empty results.
Annoyingly I can't find anywhere that specifically says whether Code Coverage works with /CLR, so I just had to try it myself.
If it should work, can anyone see what I'm doing wrong here?
[File]->[New]->[Project]
Select Class Library, enter MyProj as the project name, click OK
Right-click on MyProj project, select [Properties]
Select [Configuration Properties]->[General]
Ensure "Common Language Runtime support" is set to /CLR
Add this code to Class1:
public:
static int calc() { return 69; }
Build solution
[Test]->[New Test]->[Unit Test], click OK, click Create
Add this code to TestMethod1:
Assert::AreEqual(MyProj::Class1::calc(), 69);
Right-click on TestProject1 project, select [References]
Click "Add New Reference"
Select MyProj in the "Projects" tab, click OK, click OK again
[Test]->[Edit Test Run Configuration]->[Local Test Run]
Select [Code Coverage]
Check MyProj.dll, click Apply, click Close
[Test]->[Run]->[All Tests in Solution]
The Test Results window shows TestMethod1 has passed.
The Code Coverage Results window shows the following message:
Empty results generated: none of the
instrumented binary was used. Look at
test run details for any
instrumentation problems.
Right-click on MyProj project, select [Properties]
Select [Configuration Properties]->[General]
Change "Common Language Runtime support" to /CLR:SAFE or /CLR:PURE, click OK
Build solution
[Test]->[Run]->[All Tests In Solution]
The Test Results window shows TestMethod1 has passed.
The Code Coverage Results window now shows correct coverage information.
Based on http://msdn.microsoft.com/en-us/library/ms182534.aspx
1-project must be in debug
2.-in the project properties you must select x 86 platform.
3 Unregister the project being tested in the GAC.
Yeh, I think it all has to be in CLR:Safe project to work. I don't fully understand why, but I'm in the same boat as you.

Resources