junit.framework.AssertionFailedError: No tests found in register - selenium

I'm having a problem getting this test case to work. Can anyone point me in the right direction? I know I'm doing something wrong, I just don't know what.
import org.junit.*;
import com.thoughtworks.selenium.*;
import org.openqa.selenium.server.*;
#SuppressWarnings("deprecation")
public class register extends SeleneseTestCase {
Selenium selenium;
private SeleniumServer seleniumServer;
public static final String MAX_WAIT = "60000";
public final String CRN = "12761";
public void setUp() throws Exception {
RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.setAvoidProxy(true);
rc.setSingleWindow(true);
rc.setReuseBrowserSessions(true);
seleniumServer = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://google.com/");
seleniumServer.start();
selenium.start();
}
#Test
public void register_test() throws Exception {
//TESTS IN HERE
}
#After
public void tearDown() throws Exception {
selenium.stop();
// Thread.sleep(500000);
}
}
And I'm getting the following errors:
junit.framework.AssertionFailedError: No tests found in register
at jumnit.framework.TestSuite$1.runTest(TestSuite.java:97)
I'm stumped.

You can't both extend TestCase (or SeleniumTestCase) and also use JUnit annotations (#Test). The test runners for JUnit3 and 4 are different, and my assumption is when JUnit sees that you've extended TestCase, it uses the old runner.
Remove the #Test annotation, and instead follow the old convention of naming your test with the word "test" prefixed, and that will fix it.
public void testRegister() throws Exception {
//TESTS IN HERE
}
PS. I'd recommend following more standard Java naming conventions, such as camel casing.
PPS. Here's a link that explains this in more detail.

This means you did not created method names starting with test in following test cases class what you running currently

I was able to solve this error in my case--that is, running tests with a <junit> Ant task--by pointing to a 1.7 or later version of Ant. Ant 1.7+ honors nested <classpath> elements, in which I was pointing to a JUnit 4.x jar, which as CodeSpelunker indicated understands #Test annotations. http://ant.apache.org/faq.html#delegating-classloader provided the aha moment for me.

I'm using mockk in Kotlin for Android and I had this error.
My class was declared like this (autogenerated by Android Studio):
class MyClassTest : TestCase() {
but removing TestCase fixed the error
class MyClassTest {

Related

initializationError when running Junit test in static

I have been on a search recently for an answer to my question and I cannot seem to find it. I am trying to execute a simple test to run a JUnit test in static. When I try to execute the test I receive the following Failure.
java.lang.Exception: Method SimpleINt()should not be static.
I have JUnit 4 and hamcrest installed and pathsbuilt.
(I am still new to Selenium/Java so I am sure there is an easy explanation for this.)
package iMAP;
import org.junit.Test;
public class Test1 {
#Test
public static void SimpleINt ()
{
int i = 34;
System.out.println(i);
}
}
The JUnit documentation for #Test states:
The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case. To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. Any exceptions thrown by the test will be reported by JUnit as a failure.
So, what is implicitly said here: the expectation is that #Test is only used for non static methods.
Beyond that: don't use keywords because you can. Learn what they mean. And static is something that you rather avoid (instead of using it all over the place).
Junit Methods should not be static as much I read and study. Just delete Static and try this:
package iMAP;
import org.junit.Test;
public class Test1 {
#Test
public void SimpleINt ()
{
int i = 34;
System.out.println(i);
}
}

Assert in Selenium C#

Is there a Assert class present in Selenium C# just like we have in Coded UI test.
Or I should use the Microsoft.VisualStudio.TestTools.UnitTesting.Assert class to perform asserts in Selenium?
Yes, you would use the Assert class in your unit test framework, in your case MSTest.
The Selenium library doesn't have responsibility over test framework type of functions, including asserts.
You can use FluentAssertions which supports many different frameworks including MSTest which might minimize changes needed if you need to switch frameworks for any reason.
The Assert class is available with either the MSTest or NUnit framework.
I used NUnit, and there is the Assert class as in below line of code.
Sample code:
Assert.AreEqual(AmericaEmail, "SupportUsa#Mail.com", "Strings are not matching");
According to https://msdn.microsoft.com/en-us/library/ms182532.aspx
[TestClass]
public class UnitTest1
{
private IWebDriver driver;
[TestInitialize]
public void Setup()
{
driver = new ChromeDriver();
driver.Url = "Your URL";
}
[TestMethod]
public void TestMethod1()
{
//Your first test method
var element = driver.FindElement(By.Id("ID"));
Assert.IsTrue(element.Displayed);
Assert.AreEqual(element.Text.ToLower(), "Expected text".ToLower());
}
[TestMethod]
public void TestMethod2()
{
//Your second test method
}
[TestCleanup]
public void TearDown()
{
driver.Quit();
}
}
You can use MSTest or I would rather prefer to write simple assertion method. In Selenium, most of the use cases would be a Boolean check, IsTrue | IsFalse (you can even extend to write more complex assertions), so when you define your own assertion you will get more control of your script, like,
Taking a screenshot if the assertion fails
You can live with the failure and continue the test
You can mark the script as partially passing
Extract more information from the UI, like JavaScript errors or server errors
To use Assert you have to first create the unit testing project in Visual Studio.
Or import the following reference to the project.
using Microsoft.VisualStudio.TestTools.UnitTesting;
// Using this you can use the Assert class.
Assert.IsTrue(bool);
Assert.IsFalse(bool);
Assert.AreEqual(string,string);
Assert.Equals(obj1, obj2); // Object comparison
Assert.AreEqual(HomeUrl, driver.Url); // Overloaded, comparison. Same object value
Assert.AreNotEqual(HomeUrl, driver.Url);
Assert.AreSame("https://www.google.com", URL); // For the same object reference
Assert.IsTrue(driver.WindowHandles.Count.Equals(2));
Assert.IsFalse(driver.WindowHandles.Count.Equals(2));
Assert.IsNull(URL); Assert.IsNotNull(URL);
Selenium C# is not offering an assert class. You have to borrow it from somewhere or write your own implementation.
Writing your own implementation will give you more freedom to manage assertions, as Peter said.
Here is the basic idea. Just create a static class having assert methods like this:
public static class MyAssertClass
{
public static void MyAreEqualMethod(string string1, string string2)
{
if (string1 != string2)
{
// Throw an exception
}
}
}
MSTest Framework : Assert Class
https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.testtools.unittesting.assert?view=mstest-net-1.3.2
This way assert can be used for switching into popup's displayed:
IAlert alert = driver.SwitchTo().Alert();
String alertcontent = alert.Text;
Assert.AreEqual(alertcontent, "Do you want to save this article as draft?");
obj.waitfn(5000);
alert.Accept();

General Fixture Setup with PowerMockito and #BeforeClass

I have a test where I configure some general fixtures however after using PowerMockRule the static variables which I configure in my #BeforeClass method reset to null. This causes the following test to fail however if you remove PowerMockRule it passes.
public class Test
{
#Rule
public PowerMockRule rule = new PowerMockRule();
private static String MyString;
#BeforeClass
public static void setupClass() throws Exception
{
MyString = "FOO";
}
#org.junit.Test
public void test() throws Exception
{
assertEquals("FOO", MyString);
}
}
I have the answer, but you are not going to like it.
Short answer:it looks like a defect in PowerMock, so create a issue in our bug tracker
Long answer: As you may know the PowerMock to be able mock static, private and so on loads classes by custom class loader and modified byte code. Then #PowerMockRunneris used then PowerMock can control loading a test class and the test class is also loaded by custom class loader. In case if another jUnitRunner runs test and the PowerMockRuleis used, then the test class and all other classes that is needed for test are loaded with standard class loader. PowerMock reloads all these classes either by using deep coping with serializing/deserializing or by using objenesis. So as class is reloaded all static fields which was initialised are null.
I've briefly checked code and I haven't found test for your cases and that we processed #BeforeClass, so create a issue in our bug tracker and I'll check it deeply.
By the way, please, also point which version do you use and which dependencies do you use.

get Annotation of test method in testNG ITestListener

I am trying to integrate TestLink with TestNG
Approach is below
1>Write ITestListner with onTestFailure and onTestSuccess
2> get Annotation of the method(like testName which will be equivalent to test name in testlink) which is being failed/success in a variable
3>Make connection with TestLink using API available and update the test case.
However I am struggling to find method Annotation value in ITestListner and requirement is to get annotation values in ITestListner only so that correct test cases can be updated in Test_link
Can someone please help me how to get Test Method annotation value in ITestListner or any other approach in which i can integrate testlink update with TestNG
Hi Thanks niharika for help
,First of all you are correct in explaining use of TestNG but we are using TestNG for Selenium and already there are around 1000 test cases writen in test Methods and we have to live with that
Some how i have figured the solution ,we can still get the testName of the test method using two listners
This is just work around I am not sure if this is the best approach but as of now solving my purpose
package com.automation.testng.listner;
import org.testng.*;
public class MyIInvokeMethodListner_TestName_TestLink implements IInvokedMethodListener {
public static String testName;
public void afterInvocation(IInvokedMethod arg0, ITestResult arg1) {
// TODO Auto-generated method stub
}
public void beforeInvocation(IInvokedMethod m, ITestResult tr) {
// TODO Auto-generated method stub
//This give the Annotation Test object
org.testng.annotations.Test t=m.getTestMethod().getMethod().getAnnotation(org.testng.annotations.Test.class);
MyIInvokeMethodListner_TestName_TestLink.testName = t.testName().toString();
}
}
MyITestListner goes like below
package com.automation.testng.listner;
import org.testng.*;
public class MyITestListner_TestLink extends TestListenerAdapter {
/*IAnnotationTransformer at;
public Listner_1()
{
this.at = new Annotation_listner();
}*/
#Override
public void onTestFailure(ITestResult tr)
{
System.out.println("Hurray !I am being inboked from Test listner");
MyIInvokeMethodListner_TestName_TestLink a = new MyIInvokeMethodListner_TestName_TestLink();
System.out.println(MyIInvokeMethodListner_TestName_TestLink.testName);
}
public void onTestSuccess(ITestResult tr)
{
MyIInvokeMethodListner_TestName_TestLink a = new MyIInvokeMethodListner_TestName_TestLink();
System.out.println(MyIInvokeMethodListner_TestName_TestLink.testName);
}
}
Basically we are getting the method and then using Test Annotation class setting the static variable which can be used in MyITestListner
The ITestListener is the one which is used after <test> tag. For getting the method name and annotation specifics, you need to implement IInvokedMethodListener and in the after/before methods of this interface, and use something like method.getTestMethod().getMethodName() to get the executing method name.
If you are adding testName at the method level, I think you are doing it wrong since the help of testng mentions this "The name of the test this test class should be placed in. This attribute is ignore if #Test is not at the class level."
If you are indeed specifying the #Test at your class level then you can get it as below :
method.getTestMethod().getTestClass().getTestName()
A bit ugly and you probably want to wrap those parts in null checks in your code but this is how you get the testName specified in the annotation from the ITestResult:
iTestResult.getMethod().getConstructorOrMethod().getMethod().getAnnotation(Test.class).testName()

JUnit Test Suite for Selenium 2

I have converted a Selenium Test Suite into JUnit from Selenium IDE, and trying to execute from eclipse. But there is an error in my script in
suite.addTestSuite(Open_Google_IE.class);
suite.addTestSuite(Open_Google_FireFox.class);
Error message: The method addTestSuite(Class) in the type TestSuite is not applicable for the arguments (Class).
Please advise what could be the reason. I have also verified Creating Test Suite in Webdriver and updated test suite but still throws that error.
JUnit TestSuite
import junit.framework.Test;
import junit.framework.TestSuite;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
#RunWith(Suite.class)
#SuiteClasses(value = {Open_Google_IE.class, Open_Google_FireFox.class})
public class OpenGoogle {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTestSuite(Open_Google_IE.class);
suite.addTestSuite(Open_Google_FireFox.class);
return suite;
}
public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
}
}
As far as I can see, the code is OK.
addTestSuite() can only take classes that extend junit.framework.TestCase. Please make sure your classes extend that one, or find another way around...
your class has to extend SeleniumTestBase
like this one :
public class CreateAccountTestCase extends SeleniumTestBase {