I have a basic JUnit test which I want to run only on Linux. How I can skip the test if I build the code on Windows?
For example can I get the OS platform from Java?
System.getProperty("os.name") will give you the name of the OS. You can then use the Assume class to skip a test if the OS is Windows:
#Test
public void testSomething() {
Assume.assumeFalse
(System.getProperty("os.name").toLowerCase().startsWith("win"));
// test logic
}
Edit:
The modern JUnit Jupiter has a built-in capability for this with the #EnableOnOs and #DisableOnOs annotations:
#Test
#EnabledOnOs(LINUX)
public void testSomething() {
// test logic
}
You can also use #Before to bypass all tests contained in the class:
#Before
public void beforeMethod()
{
Assume.assumeFalse(System.getProperty("os.name").toLowerCase().startsWith("win"));
// rest of the setup
}
Related
I am building TestNG and Cucumber framework, and want to use TestNG annotations like #BeforeMethod, #AfterMethod which will run setUp and tearDown methods before and after each scenario. It works fine if I keep all my Cucumber scenarios in separate feature files. But if I have multiple scenarios in the same feature file #AfterMethod is not running after each scenario.
Also have issues if using cucumber Before and After, they work fine only if I put them in StepDefinition file below:
public class StepDefinition extends Base {
#Before
public void setUp() throws Exception {
openBrowser();
maximizeWindow();
// implicitWait(30);
deleteAllCookies();
// setEnv();
}
#After
public void quit() throws IOException, InterruptedException {
driver.quit();
}
#Given("^Navigate to ACT landing page$")
public void navigate_to_ACT_landing_page() throws Throwable {
LandingPage landingPage = new LandingPage();
landingPage.navigateToLandingPage();
}
#Then("^Verify landing page is rendered$")
public void verify_landing_page_is_rendered() throws Throwable {
LandingPage landingPage = new LandingPage();
landingPage.landingPageRendered();
}
}
But if I put them in separate class (like below) they don't work (in documentation says that they can be put anywhere in the project)
package com.act.hooks;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import java.io.IOException;
import com.moodys.act.base.Base;
public class Hooks extends Base {
#Before
public void setUp() throws Exception {
openBrowser();
maximizeWindow();
// implicitWait(30);
deleteAllCookies();
// setEnv();
}
#After
public void quit() throws IOException, InterruptedException {
driver.quit();
}
}
There are cucumber provided tags/hooks #Before and #After which runs just before and after each scenario where you can have your setup and teardown code included. Is there any specific reason you want to use testng annotations for doing so ?
This link may help for cucumber #Before and # After tags.
https://www.google.com/amp/www.automationtestinghub.com/cucumber-hooks-before-after/amp/
Cucumber does not recommend to prepare and clean states using TestNG annotations.
In order to take maximum benefits of TestNG, you can try using pure TestNG implementation for BDD with QAF. It considers each scenario as TestNGMethod.
If you want to use cucumber then you can add qaf-cucumber dependency and use TestNG factory. It will allow to use TestNG listeners and lifecycle methods (#BeforeMethod, #AfterMethod). When using cucumber with qaf you can choose either to utilize TestNG life-cycle or cucumber life-cycle.
Furthermore, if you are working with webdriver or appium it has inbuilt thread-safe driver management so you don't need to worry about driver management.
I have a selenium script written in java with the following structure
Script.java
#before
-----Some methods------
#Test
-----Some methods------
#after
-----Some methods------
and i have a main java program with structure
Main.java
public static void main(String args[]) throws IOException {
//Here i have to write the logic to run the above script
}
in which i have a main method i have to run the above mentioned script from this java program, how it can be done. as i am a newbie for java so any suggestions are welcome.
If you are using unit test framework like JUnit or TestNg Then the methods mentioned under annotations like #Test ,#Before and so on are independent itself. Their execution order is as per preferance in that framework itself. Main method not required here.
So in your case if you have done code using these methods using any TestNG or JUnit then have to use like following -
class Myclass
{
#Before
public void methodA()
{
// Your code
}
#Test
public void methodB()
{
// your code
}
#After
public void methodC
{
// your code
}
}
And Run your class like Run As > TestNG Test if you are using TestNG framework
Annotations in java needs a package like junit ,testNg or cucumber - jvm where
#Test ,#Before ,#After
could find a way to be run.So you cannot find an answer in java to run them as these annotations are not part of Java.
I think only way to run testNG based script is to create a suite of testNG.xml file and do the execution of suite in main class.Try this code
List<String> suitesList = new ArrayList<String>();
TestListener listener = new TestListener();
TestNG testng = new TestNG();
testng.setOutputDirectory("outputfoldername");
suitesList.add("testng.xml");
testng.setTestSuites(suitesList);
testng.addListener(listener);
testng.run();
I have some own components, which I start before my Java application. It takes about 30 seconds to start this. In my integration tests, I start my components before the class and all test cases run. My question is, is it possible to run my components not before the test class, but rather before the whole test?
kind regards,
bilal
If you use JUnit suites you can use a #BeforeClass to execute a method before the entire suite runs and an #AfterClass after the entire suite runs:
#RunWith(Suite.class)
#SuiteClasses(
{
//list your test classes here
}
)
public class IntegrationSuite{
#BeforeClass
public static void setupSuite(){
//do your initialization here
}
#AfterClass
public static void tearDownSuite(){
//...if needed
}
}
Use the #BeforeClass annotation.
Please note that the annotated method has to be static.
#BeforeClass
public static void oneTimeInit() {
System.out.println("It runs only once for all tests in this class.");
}
If you are using maven you could use the maven-failsafe-plugin. It has a pre-integration-test goal intended for test setup. For an example take a look at Maven Failsafe Plugin: how to use the pre- and post-integration-test phases
I'm working on writing unit tests for a class that I'm developing. Another developer is developing other tests for the same class for methods that he's developing. So our tests find themselves in the same JUnit test class.
So what I wanted to do was to set up a test suite to run just my tests while I'm developing as a temporary measure. I created a Category for my tests and have marked them as such. I then created a class to be my test suite. I told it to include tests that belong to this category. When I run it, it still runs everything. There are a lot of tests, so it would be tedious to mark all the tests I don't want ran with #Ignore. Is there a way to say, run only the tests in a category but none else?
You can write a wrapper test class which method calls the main test class (only your method), then run Junit tests on the wrapper class.
public class MainTestClass {
#Test
public void yourFirstTest() {
...
}
#Test
public void yourSecondTest() {
...
}
#Test
public void otherFirstTest() {
...
}
}
public class WrapperTestClass {
#Test
public void yourFirstTest() {
new MainTestClass().yourFirstTest();
}
#Test
public void yourSecondTest() {
new MainTestClass().yourSecondTest();
}
}
I think you can implement your own 'org.junit.runner.RunWith' and then annotate your test class to use it as necessary.
#RunWith(MyRunnerClass.class)
Note: The correct solution here is in the above comments regarding code branches etc.
I am writing some junit tests in eclipse and I need to do some time consuming setup before the tests. Appeared that #BeforeClass should be the way to do this. I currently tested this on a class that has 2 #Test functions.
When I right click on a class in eclipse and chose "Run As" -> "JUnit Test" I can see that the #BeforeClass is executed before both functions.
I even tried to change #BeforeClass to #Before and stored in a boolean whether we had already executed this function, but it seems that eclipse created two class objects from the same class, one for each test to run so that did not help either.
So what should I do to have a setup function run only one time even if I have many tests ? Or am I just using eclipse incorrectly when trying to run the tests ?
The setup is something like this:
public class SuperClass {
#BeforeClass
public void { // do timeconsuming setup }
}
public class TestClass extends SuperClass {
#Test
public void test1() { // perform first test }
#Test
public void test2() { // perform second test }
}
Making static the method annotated with BeforeClass may be the solution:
#BeforeClass
public static void
#BeforeClass methods should be static in order to be executed only once.