Showing JUnit view in Eclipse when running tests from Code - java

When I run just my Testclass in Eclipse I get the JUnit view showing the tree structure and if the test was successful. If I start my Test from code:
JUnitCore core = new JUnitCore();
core.run(SimpleTests.class);
the view does not show. Can I change this?

On your toolbar click Windows-->Show View-->Others.
Type "Junit" without quotes. Select from list and click OK.

I suppose you run your code in the main method like this example :
public class MiniTest extends TestCase
{
#Test
public void test()
{
System.out.println("Running test");
}
public static void main(String[] args)
{
JUnitCore core = new JUnitCore();
core.run(MiniTest.class);
}
In this case, it will not show your view because you launch your program as a java application (alt-shift-X-J). You will see "Running test" in the console but that is.
If you want to see the Junit View you must launch your program as a Junit test (alt-shit-X-T).
Note that with this manner the main() is not necessary, Eclipse will directly launch the method tagged with #Test. You can also group tests with #Suite.

You can't interact with the JUnit view from your code using JUnitCore.
You can however, add your tests to the JUnitView if you use a #Parameterized test, or if you implement your own test runner. Try extending the Suite class and reimplement one of the constructors with the tests that you want to execute.

I get this problem from time to time.
There is something corrupted in the eclipse workspace.
The only solution for me is to create a new workspace and bring in the projects.
It's a serious pain when there are fifty projects in the workspace.

If the view doesn't show, go to Window -> Perspective -> Reset Perspective..

Related

NetBeans : JUnit no tests executed

So I'm getting this persistent error using netbeans. I've got a LinkedList class which I am testing via a JUnit test, which I created by clicking on LinkedList.java: Tools -> Create/Update Tests and this LinkedListTest.java class is now located in test packages.
My LinkedList.java file works correctly when tested in a file with a main method.
public class LinkedListTest {
#Test
public void testAddFirst() {
LinkedList linkedList = new LinkedList();
Country c1 = new Country("Australia");
linkedList.addFirst(c1);
assertEquals("Australias", linkedList.getValue(0)); // Should fail a test
} // default test methods beneath
All my imports check out. JUnit 5.3.1 and I had to download apiguardian1.1.0.jar from MVN repository to clear an error for:
reason: class file for org.apiguardian.api.API$Status not found
I right-click in this file and select Test File, or use Ctrl+F6, I've selected Test File from the original LinkedList file, I've even used Alt+F6 which tests the whole project. Yet I'm met with 'No tests executed.', an empty Test Results window, and no Notifications. What am I doing wrong?
Thanks for any help
Edit: I just switched from netbeans to eclipse.
You forget to extend Runner with class --
use like below with class -
public class LinkedListTest extends Runner {
}
Hope this help you.

Allure java Run specific tests depending on story or feature

I have two methods in my test class:
#Test
#Stories( "story1")
public void test01(){
}
#Test
#Stories( "story2")
public void test02(){
}
#Test
#Stories( "story1")
public void test03(){
}
To run tests Im using:
mvn clean test site
It will execute all test. But my question is, how to execute tests when I want to execute only tests with specific user story (ie. story1)
I know in python it can be done by
py.test my_tests/ --allure_stories=story1
But I don't know how to do it in java using maven
In Java there is no need for Allure to do such sort of things, because you can do it using your test runner, e.g. TestNG.
Just create Listener or BeforSuite which will check your environment variable e.g. -DallureStories and match it with ITestContext to disable tests not in your stories list.

Junit - Override default behaviour to run tests only once, even if they are added several times (in different Suites)

For our usecase, as an example - we need to run a JUnit test, even if it is added multiple times within a Test Suite, without being skipped.
Currently we notice that JUnit test runner skips a Test with the same name, if it finds the test somewhere else within a Test Suite. Here is an example screenshot to show test "Case_A" within "Procedure_A" being skipped within a Test Suite -
Could this behaviour be overriden, if so could someone point us in the right direction?
I did some research arround this problem.
Simple setting - one Test "TestCase_A" and one Suite "TestProcedure_A" that runs the TestCase_A twice :
public class TestCase_A {
#Test
public void test() throws Exception {
System.out.println("Case_A RUN");
Assert.assertTrue(true);
}
}
#RunWith(Suite.class)
#Suite.SuiteClasses({ TestCase_A.class, TestCase_A.class })
#SuppressWarnings("all")
public class TestProcedure_A {
}
I run the test Suite using Eclipse and maven.
Finding: The sysout statement actually shows, that the TestCase_A runs twice!
Therefore, the Eclispe View is misleading. Test are run multiple times - the tree also reflects this. However, the status of the actual single calls is not displayed properly in the Eclispe Junit View.
I presume the view is based on the junit.runner.TestRunListener. It probably worth looking into that.

Android : Espresso UI testing, what is the right approach to test classes structure?

I'm trying to start with automatization testing of the UI in Android APP. I choosed espresso FW for this and i would like to ask on this:
What is the right structure of the test classes? It means, should i create a new testClass for each activity and run all in batch or i should have testing class for some scenarios across the whole app?
And how can i run all tests in package at once? Because now i must right-click on the test class and select run test class manually for each test.
Thanks for any advice.
Note:
Example of the very simple scenario i did by this way:
#RunWith(AndroidJUnit4.class)
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
private MainActivity mActivity;
public MainActivityTest() {
super(MainActivity.class);
}
#Before
public void setUp() throws Exception {
super.setUp();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
mActivity = getActivity();
}
#Test
public void buttonShouldUpdateText(){
onView(withId(R.id.goToSecondActivityBtn)).perform(click());
onView(withId(R.id.text1SecondView)).check(matches(withText("Hello world!")));
onView(withId(R.id.txtFieldOne)).perform(typeText("TEST"));
}
}
But if i addded second method into class which is processing some interaction on the second activity i failed with the exception.
To run all tests (assuming you have a Gradle configuration) use the Gradle task connectedCheck.
In Android Studio you can select it from the gradle tasks list on your right panel and from command line:
./gradlew connectedCheck
As for testing structure, assume in your example test class that every #Test method restarts your activity (in the example MainActivity). If you really want to test only the second Activity you should create a new test class, even if it's just to organize test cases in a cleaner way.
For a more advanced test architecture using Espresso I recommend this article:
https://medium.com/#neoranga55/the-evolution-journey-of-android-gui-testing-f65005f7ced8
And the demo code:
https://github.com/neoranga55/CleanGUITestArchitecture

A better way to run lots of Integration Tests using JUnit?

What is the best way run a lot of integration tests using JUnit?
I crudely discovered that the code below can run all the tests... but it has a massive flaw. The tearDown() method in each of those classes is not called until they have all been run.
public class RunIntegrationTests extends TestSuite {
public RunIntegrationTests(){
}
public static void main (String[] args){
TestRunner.run(testSuite());
}
public static Test testSuite(){
TestSuite result = new TestSuite();
result.addTest(new TestSuite(AgreementIntegrationTest.class));
result.addTest(new TestSuite(InterestedPartyIntegrationTest.class));
result.addTest(new TestSuite(WorkIntegrationTest.class));
// further tests omitted for readability
return result;
}
}
The classes being run connect to the database, load an object, and display it in a JFrame. I overrode the setVisible method to enable testing. On our build machine, the java vm runs out of memory when running the code above as the objects it has to load from the database are pretty large. If the tearDown() method was called after each class finished it would solve the memory problems.
Is there a better way to run them? I'm having to use JUnit 3.8.2 by the way - we're still on Java 1.4 :(
Not sure if this is the problem, but according to the JUnit Primer you should just add the tests directly, instead of using the TestSuite:
result.addTest(new AgreementIntegrationTest()));
result.addTest(new InterestedPartyIntegrationTest()));
result.addTest(new WorkIntegrationTest()));
That's very strange. setUp and tearDown should bookend the running of each test method, regardless of how the methods are bundled up into suites.
I typically do it slightly differently.
TestSuite suite = new TestSuite( "Suite for ..." ) ;
suite.addTestSuite( JUnit_A.class ) ;
suite.addTestSuite( JUnit_B.class ) ;
And I just verified that tearDown was indeed being called the correct number of times. But your method should work just as well.
Are you sure tearDown is properly specified -- e.g. it's not "teardown"? When you run one test class on its own, is tearDown properly called?

Categories

Resources