how to avoid any further testing if no connection

43 views
Skip to first unread message

mike

unread,
Mar 12, 2013, 5:33:32 AM3/12/13
to testng...@googlegroups.com
Hi,
 
We need to write a test case where a precondition is to connect to a server ( different servers depending on user).
 If there is no connection it shall not run any testcases ( so testcases should not fail since there were no server to connect to). It should merly run skip them.
Is it possible to do in testng?
 
br,
 
//mike
 

Krishnan Mahadevan

unread,
Mar 12, 2013, 5:35:54 AM3/12/13
to testng...@googlegroups.com
Why not incorporate the connection logic in an @Before[Suite/Test/Class] method? Wouldn't that help?

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 


--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

mike

unread,
Mar 12, 2013, 11:10:33 AM3/12/13
to testng...@googlegroups.com
Hi,
 
That works.
 
How can I avoid that this class is not executed by maven ( since we don't want this to be part of the normal delivery process.
This test should be performed manually ( but still be testng). Is it possible?
 
br,
 
//mike

Den tisdagen den 12:e mars 2013 kl. 10:35:54 UTC+1 skrev Krishnan:
Why not incorporate the connection logic in an @Before[Suite/Test/Class] method? Wouldn't that help?



On Tuesday, March 12, 2013, mike wrote:
Hi,
 
We need to write a test case where a precondition is to connect to a server ( different servers depending on user).
 If there is no connection it shall not run any testcases ( so testcases should not fail since there were no server to connect to). It should merly run skip them.
Is it possible to do in testng?
 
br,
 
//mike
 

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@googlegroups.com.

To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Krishnan Mahadevan

unread,
Mar 12, 2013, 11:14:46 AM3/12/13
to testng...@googlegroups.com
Mike,
I can suggest a couple of ways in which you can get this done:

1. Use a Listener to get this done [ISuiteListener (or) ITestListener]. That way you can decide to include/exclude a reference to this listener in your testng xml file.
2. If you used an @Before[Suite/Test/Class] annotation to get this done, plug this logic into a condition which basically fetches the value of a VM arg. If the VM arg is set then run the logic if not merely skip it.

Would that work ?

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.

mike

unread,
Mar 12, 2013, 12:42:06 PM3/12/13
to testng...@googlegroups.com
Hi Krishnan,
 
I think I will try option 2 since I used @BeforeClass and check if vm arg is set.
What do you mean by skip it if vm args is not set?
 
br,
 
//mike

Krishnan Mahadevan

unread,
Mar 12, 2013, 12:49:37 PM3/12/13
to testng...@googlegroups.com
Mike,

Here's what I was hinting at :


public class MyTestClass{

    @BeforeTest
     public void myTestSetup(){
     String shouldRunSetup = System.getProperty("runSetup");
     boolean runSetup = true;
     if (shouldRunSetup != null && ! shouldRunSetup.trim().isEmpty()){
         runSetup = Boolean.parseBoolean("shouldRunSetup"); 
      }
      if (runSetup){
          //your setup logic goes here
      }
}





Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


mike

unread,
Mar 12, 2013, 1:29:58 PM3/12/13
to testng...@googlegroups.com
Hi,

It still tries to execute the testcase and it will of course fail. Here is the code I am using as setup:

@BeforeClass
    public void beforeClass() throws Exception {
        boolean run = false;
      
        String host= getProperty("host");
        if (host != null && !host.trim().isEmpty()) {
            run = true;
        }
        if (run) {
           
            connect();

          
        }

    }

@Test
public void  myTestCase() throws Exception{

}

This myTestCase will still be executed....

What am I missing?

br,

//mike

Krishnan Mahadevan

unread,
Mar 12, 2013, 1:33:09 PM3/12/13
to testng...@googlegroups.com
Mike,
Try to have your connect() method throw an exception [ just to see that what I am suggesting will work here ]. This exception will automatically cause the @Before method to fail. Once the configuration method fails, the tests will automatically be skipped.


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


mike

unread,
Mar 12, 2013, 1:45:30 PM3/12/13
to testng...@googlegroups.com
I throw exception and I get the following:

Default suite
Total tests run: 1, Failures: 0, Skips: 1

But when I run it from maven:

Failed tests:
  beforeClass(com.mycomparny.NewTest)

Tests run: 100, Failures: 1, Errors: 0, Skipped: 2

This is strange any ideas?

br,

//mike

Krishnan Mahadevan

unread,
Mar 12, 2013, 1:47:30 PM3/12/13
to testng...@googlegroups.com
Mike,
@BeforeClass is effective ONLY when that particular test class gets picked up (instantiated by TestNG) for running your tests.

If you would like this to be effective at your suite level then you might want to consider moving this to either @BeforeTest [ executes before the <test> gets executed] or @BeforeSuite [ executes before the <suite> gets executed ]


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


mike

unread,
Mar 12, 2013, 1:54:03 PM3/12/13
to testng...@googlegroups.com
I tried switching from @BeforeClass to @BeforeTest/@BeforeSuite but I get the same result.
So there is no difference in the execution result as before.

br,

//mike

mike

unread,
Mar 13, 2013, 3:46:41 AM3/13/13
to testng...@googlegroups.com
The difference that I see is that testng ( using Eclipse plugin):

[TestNG] Running:
  /tmp/testng-eclipse-1097883419/testng-customsuite.xml

FAILED CONFIGURATION: @BeforeClass beforeClass
java.lang.Exception: Couldn't find a defined '{}', please start the program
    at com.ericsson.commonlibrary.nealservices.manual.NewTest.getProperty(NewTest.java:113)
    at com.ericsson.commonlibrary.nealservices.manual.NewTest.beforeClass(NewTest.java:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
    at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:175)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:107)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1203)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
    at org.testng.TestNG.run(TestNG.java:1036)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

SKIPPED: testmyServerConnection

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 1
    Configuration Failures: 1, Skips: 0
===============================================


===============================================

Default suite
Total tests run: 1, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 0
===============================================

Maven does not distinguish between Test Failures and  Configuration failures. So it will still report test as a failure.
Any way around this?

br,

//Mike

mike

unread,
Mar 13, 2013, 8:33:48 AM3/13/13
to testng...@googlegroups.com
I solved it buy using groups on the manual testcase and exclude it in my pom.xml usinh

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.4.2</version>
                <configuration>
                    <excludedGroups>[name of group here!]</excludedGroups>
                </configuration>
            </plugin>

br,

//mike
Reply all
Reply to author
Forward
0 new messages