apache / jmeter

Apache JMeter open-source load testing tool for analyzing and measuring the performance of a variety of services
https://jmeter.apache.org/
Apache License 2.0
7.98k stars 2.03k forks source link

Support for JUnit 4.x tests defined by annotations #2273

Closed asfimport closed 14 years ago

asfimport commented 14 years ago

Brian (Bug 47803): The current version of jmeter doesn't recognize test cases specified by the @Test annotation for junit 4.x+. Support should be easy to support since junit 3 is already supported. Basically, an annotated test case sampler should look for the @Test annotation instead of classes that extend TestCase.

That's what I've done in the attached patch. The annotated sampler is an extension of the 3.x sampler. I've overridden the sample method and a couple of support methods to work with the new style tests. That causes a difference in behavior between instances of the 3.x and annotated samplers. I initially wanted to share the UI code and have it intelligently switch between 3.x and annotated instances, but the methods available didn't easily permit that. The choice was between adding a very limited new UI type or hiding the actual junit sampler instance behind a facade. It seemed more in keeping with other patterns to just make another UI class. That seems like a reasonable choice given that I expect users will know what they're trying to test (and which version of junit they depend on) before they start writing test plans.

There are 2 remaining details where I could use some help. First, I've added a resource key (junit_4_request=JUnit Annotated Request) that needs to be translated. I'm not sure what the process for that is. Second, in the saved test plan files, saves of my added class use the fully qualified name. Other classes seem to use short names. Everything seems to work okay for me, but there's probably a reason it looks different. I imagine that should be fixed.

Created attachment JUnitAnnotations.patch: Support for junit annotated test cases

OS: All

asfimport commented 14 years ago

Brian (migrated from Bugzilla): I forgot to mention that for this to all work, you'll need to be using a version of junit 4 instead of the junit 3.8.2 that normally comes with the current release of jmeter.

asfimport commented 14 years ago

Sebb (migrated from Bugzilla): Thanks for the patch; I'll take a look at adding it soon.

Would it be possible to provide some example tests as well?

asfimport commented 14 years ago

Brian (migrated from Bugzilla): Yes - working on that. But in doing so I realized that some of the changes that I made busted things for new test plans (I think my saved test plans that I was using to make sure I didn't break things were working - leading me astray). So I'm going to fix those issues today and re-submit. I'll tack a test case on there.

asfimport commented 14 years ago

Brian (migrated from Bugzilla): My previous test included the prefix "test" on methods, which is why I didn't notice things were a bit broken. This includes a fix that allows arbitrarily named methods.

@Suite, @BeforeClass and @AfterClass aren't currently supported. It doesn't really seem to fit in with the framework of 1 test per sampler.

Created attachment annotations.patch: updated annotations patch

asfimport commented 14 years ago

Brian (migrated from Bugzilla): Annotated test to exercise the new code. If you don't run setup/teardown on the add() test, it should fail. If you do run setup/teardown, it should work.

Created attachment DummyAnnotatedTest.java: test case using JUnit 4.x annotations

DummyAnnotatedTest.java ````java package test; import org.junit.After; import org.junit.Before; import org.junit.Test; public class DummyAnnotatedTest { public String name; public int two = 1; //very wrong. public DummyAnnotatedTest() { name="NOT SET"; } public DummyAnnotatedTest(String name) { this.name = name; } @Test(expected=RuntimeException.class) public void fail() { throw new RuntimeException(); } @Before public void verifyTwo() { two = 2; } @After public void printDone() { System.out.println("done with an annotated test."); } @Test public void add() { int four = two+2; if(4!=four) { throw new RuntimeException("4 did not equal four."); } //or if you have assertions enabled assert 4 == four; } } ````
asfimport commented 14 years ago

Sebb (migrated from Bugzilla): Thanks very much!

Two minor problems:

Would you mind fixing those please?

Also, if you get a chance, perhaps you could add some more Test annotations, e.g. expectedException and timeout, since these are handled by the code?

asfimport commented 14 years ago

Brian (migrated from Bugzilla): Should flex the abilities of the patch that adds support for junit 4.x style tests

Created attachment DummyAnnotatedTest.java: annotated test cases

DummyAnnotatedTest.java ````java /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package test; import org.junit.After; import org.junit.Before; import org.junit.Test; public class DummyAnnotatedTest { public String name; public int two = 1; //very wrong. public DummyAnnotatedTest() { name="NOT SET"; } public DummyAnnotatedTest(String name) { this.name = name; } @Test(expected=RuntimeException.class) public void fail() { throw new RuntimeException(); } @Before public void verifyTwo() { two = 2; } @After public void printDone() { System.out.println("done with an annotated test."); } @Test public void add() { int four = two+2; if(4!=four) { throw new RuntimeException("4 did not equal four."); } //or if you have assertions enabled assert 4 == four; } //should always fail @Test(timeout=1000) public void timeOut() { try{ Thread.sleep(2000); }catch (InterruptedException e) { } } } ````
asfimport commented 14 years ago

Brian (migrated from Bugzilla): Fixes a timeout bug.

Created attachment annotations.patch: updated annotations patch

asfimport commented 14 years ago

Brian (migrated from Bugzilla): Added the apache license to another file where I'd left it off.

Created attachment annotations.patch: patch to support junit 4.x style annotated tests

asfimport commented 14 years ago

Brian (migrated from Bugzilla):

Thanks for looking the patch over.

asfimport commented 14 years ago

Sebb (migrated from Bugzilla): Thanks very much for the patches.

It was reasonably easy to merge the JUnit4 code with the existing JUnit3 code, so I just added a checkbox to the GUI to allow users to select JUnit4 rather than JUnit3 test cases. I think this is easier than having two separate samplers.

SVN checkins:

https://github.com/apache/jmeter/issues/2273 - Support for JUnit 4.x tests defined by annotations Merge code from JUnitAnnotatedSampler.java

URL: http://svn.apache.org/viewvc?rev=817404&view=rev Log: https://github.com/apache/jmeter/issues/2273 - Support for JUnit 4.x tests defined by annotations Fix bug: test element not updated if method/classname not defined

URL: http://svn.apache.org/viewvc?rev=817397&view=rev Log: https://github.com/apache/jmeter/issues/2273 - Support for JUnit 4.x tests defined by annotations Sample test cases

URL: http://svn.apache.org/viewvc?rev=816976&view=rev Log: Tidy up. Add hooks for JUnit4 GUI support