Does Jmeter supports Junit testsuite?
This question trouble me for several days, the test cases all working well no matter a style of junit 3 or 4. But the testsuite is anyway dumb.
Any suggestions?
My code below:
public class LoginLogout extends TestCase {
private static Logger log = Logger.getLogger(LoginLogout.class);
public static Test suite() {
try{
log.info("test suite start!");
TestSuite suite = new TestSuite(LoginLogout.class.getName());
//$JUnit-BEGIN$
suite.addTestSuite(Login.class);
suite.addTestSuite(Logout.class);
return new TestSetup(suite) {
protected void setUp(){
log.info("test suite setup!");
}
protected void tearDown(){
log.info("test suite finished!");
}
};
}catch(Exception e){
log.error(e.getMessage());
}
return null;
}
}
public class Login extends TestCase {
private static Logger log = Logger.getLogger(Login.class);
#Test
public void testLogin() throws Exception {
log.info("login start!");
log.info("login end!");
}
}
public class Logout extends TestCase {
private static Logger log = Logger.getLogger(Logout.class);
#Test
public void testLogout() throws Exception {
log.info("logout start!");
log.info("logout end!");
}
}
You can go to "Download Apache JMeter" page on http://jmeter.apache.org/ , and download the "apache-jmeter-2.8_src.zip" (or whatever the current version is).
After unzipping it, under apache-jmeter-2.8_src\apache-jmeter-2.8\src\junit\test directory, you can find the following java files (as for jmeter version 2.8):
For JUnit4:
AfterAnnotatedTest.java
BeforeAnnotatedTest.java
DummyAnnotatedTest.java
Junit4AnnotationsTest.java
For JUnit3:
RerunTest.java
SetupTestError.java
SetupTestFail.java
TearDownTestFail.java
You can see them shown up at the Classname dropdown menu on JUnit Request of JMeter (Test Plan --> Thread Group --> JUnit Request).
Those JUnit test cases are provided by JMeter by default, so I assume that a simple copy-and-paste of their code and work from there should work; however, so far, I am not able to see my test cases shown up at the Classname dropdown menu.
Here are other useful links I have found; however, none of them solves the current problem I am encountering:
Running Selenium scripts from Jmeter
http://jmeter.apache.org/usermanual/junitsampler_tutorial.pdf
JUnit test classes not showing up in JMeter
For JUnit4 the Suite would be:
#RunWith(Suite.class)
#SuiteClasses({Login.class, Logout.class})
public class LoginLogout {
private static Logger log = Logger.getLogger(LoginLogout.class.getName());
}
And the TestClass is:
public class Login {
private static Logger log = Logger.getLogger(Login.class.getName());
#Test
public void testLogin() throws Exception {
log.info("login start!");
log.info("login end!");
}
}
Worked fine for me
Related
I am trying to add ExtentReport in my test automation project, but IntelliJ is throwing this error at line "report = ..." inside "BeforeMethod"
Expected 0 arguments but found 1
I am trying to insert the location of the report file. Help much appreciated. Thanks.
public class Methods {
public static WebDriver driver;
public static ExtentReports report;
public static ExtentTest test;
#BeforeTest
public void startReport(){
report = new ExtentReports(System.getProperty("C:\\Program Files (J)\\VCWebMaine\\Report.html"));
}
#BeforeMethod
public void setDriver() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (J)\\VCWebMaine\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://xxxxxxxxxxxxxxxx");
Thread.sleep(2000);
}
I tried looking for answers in Google. It looks like the same lines work in Eclipse. Is it a problem in the IDE?
Other part of the script as below:
public class TestCases extends Methods {
ExtentTest test = Methods.test;
#Test
public void scenario01() throws InterruptedException, IOException {
I am setting up an automated framework to run tests on Android emulators, using Appium. I have added logic to launch Appium and the emulator programatically, but would like to be able to edit the "launch settings" from the TestRunner class.
My ideal goal is to have everything I need in the TestRunner class, so I can run my tests against a specific port, emulator, and tags.
But currently with the method I have now, I am receiving the following error:
'Message: cucumber.runtime.CucumberException: Hooks must declare 0 or 1 arguments.'
#CucumberOptions(
plugin = {"pretty", "html:target/cucumber-reports"}
, monochrome = true
, features = "src/test/java/feature"
, tags = "#Login"
)
public class TestRunner {
public void run() throws MalformedURLException, InterruptedException {
setUpDriver(4723, "Android9");
}
}
_________________________________________________________
public class Hooks extends DriverFactory {
static AppiumDriverLocalService service;
#Before
public static void setUpDriver(int port, String simulator) throws InterruptedException {
service = AppiumDriverLocalService
.buildService(new AppiumServiceBuilder().usingPort(port)
.usingDriverExecutable(new File("path/to/node/file"))
.withAppiumJS(new File("/path/to/appium/file")));
System.out.println("\n Appium server: " + service.getUrl());
service.start();
Thread.sleep(2000);
try {
setUpMobileDriver(simulator);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
You can pass from Maven or you can use System properties
Issue : When running integration tests from maven (mvn verify) the spring application context is not initialized properly, it doesn't take in consideration my custom ApplicationContextInitializer class.
Test Class :
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = {MainApplication.class}, initializers = CustomContextInitializer.class)
#WebIntegrationTest
public class ApplicationIT {
// Running a SOAPUI suite as a JUnit Test
#Test
public void TestGateway() throws Exception {
SoapUITestCaseRunner runner = new SoapUITestCaseRunner();
runner.setProjectFile("../gateway/src/test/resources/soapui/gateway-soapui.xml");
runner.run();
}
}
MainApplication class :
#Configuration
#ComponentScan(basePackages = {
// different packages here (not relevant)
})
#EnableAutoConfiguration
public class MainApplication {
public static void main(String[] args) throws Exception {
new SpringApplicationBuilder(MainApplication.class)
.initializers(new CustomContextInitializer())
.run(args);
}
}
CustomContextInitiliazer class (for adding custom .properties files to the spring environment application context) :
public class CustomContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext>{
#Override
public void initialize(ConfigurableApplicationContext applicationContext) {
ConfigurableEnvironment env = applicationContext.getEnvironment();
try {
Resource[] res = new PathMatchingResourcePatternResolver().getResources("classpath*:/*.properties");
for (Resource re : res) {
env.getPropertySources().addFirst(new ResourcePropertySource(re));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Results :
1) Everything works on when I start and run the application (either from IDE or by invoking mvn exec).
2) Integration tests run ok when started from IDE.
3) Integration tests throw error when invoked via maven verify because the custom properties files are not loaded into spring context environment. The result is the same as if I wouldn't have written initializers = CustomContextInitializer.class in the test class and tried to run the tests from IDE.
I think your code is correct, but your .properties files may be at the wrong place. Make sure they are under <project>/src/main/resources or that you have configured a custom resource folder in maven. If they reside under <project>/src/main/java they will not be part of the classpath as far as maven is concerned.
I'm trying to run Selenium RC 1.0.3 using Java 6, JUnit 4, and Eclipse on Snow Leopard.
Here is my test class, from the Selenium docs:
public class TestCase extends SeleneseTestCase {
#Before
public void before() throws Exception {
setUp("http://www.google.com/", "*firefox");
}
#Test
public void test() {
selenium.open("/");
selenium.type("q", "selenium rc");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
assertTrue(selenium.isTextPresent("Advanced search"));
}
}
I receive the following error, which occurs at the time that selenium.open() is called:
11:16:59.916 INFO - Got result:
XHR ERROR: URL = http://localhost:4444/ Response_Code = 403
Error_Message = Forbidden+for+Proxy on session a8cf1e0bd5ed42c5a4df0c25ec5f5286
I've tried (finding various suggestions on the web) replacing *firefox with *chrome or *firefox, replacing http with https and adding selenium.start(), but none have helped, or even changed the behavior.
Any ideas?
EDIT: The selenium-server is running, and the local firewall is disabled.
OK, here's a solution, without any understanding: If the #Before method is removed, and the call to setUp() is moved into the #Test method, then it works:
#Test
public void test() throws Exception {
setUp("http://www.google.com/", "*chrome");
selenium.open("/");
selenium.type("q", "selenium rc");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
assertTrue(selenium.isTextPresent("Advanced search"));
}
But here is a better solution, based on understanding:
import com.thoughtworks.selenium.SeleneseTestCase;
public class TestCase extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("http://www.google.com/", "*firefox");
}
public void testAuto() throws Exception {
selenium.open("/");
selenium.type("q", "selenium rc");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
assertTrue(selenium.isTextPresent("Advanced search"));
}
}
It turns out that SeleneseTestCase extends TestCase from JUnit 3. I had upgraded the documentation example to JUnit 4 without thinking about what problems may be caused.
My unit tests are in a separate directory tree from my integration tests, but with the same package structure. My integration tests need external resources (e.g. a server) to be available, but my unit tests are properly independent of each other and the environment.
In IntelliJ-IDEA (v7) I have defined a JUnit Run/Debug Configuration to run all the tests in the top-level package, and this of course picks up my integration tests which fail.
I want to define a run-junit configuration that runs all my unit tests. Any ideas?
The answer is to create a test suite that contains only those tests underneath the unit test folder and run that instead. There is a junit-addon which does just this called DirectorySuiteBuilder but I only found this after I had pretty much re-invented the wheel.
And it's already been asked here!
import junit.framework.JUnit4TestAdapter;
import junit.framework.TestSuite;
import java.io.File;
import java.io.IOException;
public class DirectoryTestSuite {
static final String rootPath = "proj\\src\\test\\java\\";
static final ClassLoader classLoader = DirectoryTestSuite.class.getClassLoader();
public static TestSuite suite() throws IOException, ClassNotFoundException {
final TestSuite testSuite = new TestSuite();
findTests(testSuite, new File(rootPath));
return testSuite;
}
private static void findTests(final TestSuite testSuite, final File folder) throws IOException, ClassNotFoundException {
for (final String fileName : folder.list()) {
final File file = new File( folder.getPath() + "/" +fileName);
if (file.isDirectory()) {
findTests(testSuite, file);
} else if (isTest(file)) {
addTest(testSuite, file);
}
}
}
private static boolean isTest(final File f) {
return f.isFile() && f.getName().endsWith("Test.java");
}
private static void addTest(final TestSuite testSuite, final File f) throws ClassNotFoundException {
final String className = makeClassName(f);
final Class testClass = makeClass(className);
testSuite.addTest(new JUnit4TestAdapter(testClass));
}
private static Class makeClass(final String className) throws ClassNotFoundException {
return (classLoader.loadClass(className));
}
private static String makeClassName(final File f) {
return f.getPath().replace(rootPath, "").replace("\\", ".").replace(".java", "");
}
}
IntelliJ IDEA CE 10.5 has a (new?) option to run all tests inside a configured directory:
Unfortunately there's no way to separate the output from the IntelliJ compile other than by classes and test classes within a single module (it's the classes that test runner is looking at).
So when I have integration tests I simply use a second module specific to these tests to get round this problem, specifying output directories as necessary for each module.