Running a java script with a java program - java

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();

Related

System.out.print() prints unit test logs to output

i have a problem which occurs when using NetBeans 8.2 and JUnit.
Considering the following example:
Unit Test:
private MyClass myClass = new MyClass();
#Test
public void testSomething() {
myClass.testMethod();
}
// OTHER TEST EXISTING BUT THESE ARE TAGGED WITH #IGNORE
If I am using the following production code:
MyClass:
public class MyClass {
public void testMethod() {
System.out.print("test");
}
}
The "Unit test output" in NetBeans displays something like:
test
Testcase: testSomethingOther(mypackage.MyClassTest):SKIPPED
Testcase: testSomethingOther2(mypackage.MyClassTest):SKIPPED
-- other tests which are ignored / failed --
But I only want NetBeans to display my own "print" statements, and not some Test details.
If i am using the "println" function, it works as as planned (no test details are written to output console).
What is the problem here?
You're using the annotation #Test which implies that you're probably using JUnit, TestNG or another Testing framework. If you don't want to see your framework's prints either check if there's a configuration setting / flag that you can pass that turns it off or stop using it and find another way to run your tests.

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.

Run JUnit test only on Linux

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
}

How do I make a junit white list of tests to run

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.

#BeforeClass runs multiple times for the same class in eclipse

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.

Categories

Resources