TestNG shows 0 Test run

痞子三分冷 提交于 2019-12-17 22:28:07

问题


I'm trying to execute my test scripts using testNG and trying the below code, but 0 displayed against run, fail and skip in the console. Because of that I'm unable to verify the results in my script.

Java:

package com.demoaut.newtours.testcases;
import org.testng.Assert;
import org.testng.annotations.Test;

//import junit.framework.Assert;
public class TC002_CheckAssert {
    @Test
    public TC002_CheckAssert() {
        System.out.println("ajkcbh");
        try {
            Assert.assertEquals("Pass", "Pass");
        }
        catch(Exception e) {
            System.out.println("Exception:" + e.getLocalizedMessage());
        }
    }
}

I am executing the above script through testng.xml file.

<suite name="Suite">
  <test name="Test">
   <classes>
      <class name="com.demoaut.newtours.testcases.TC002_CheckAssert" />
   </classes>
  </test>
</suite>

Console results:

ajkcbh

"==============================================="
Suite
Total tests run: 0, Failures: 0, Skips: 0
"==============================================="

回答1:


Here is the Answer to your Question:

There is a minor bug in your code block. When you are using TestNG and writing methods within @Test annotation, we should define the methods with proper return types. I have used your own code and simply added the return type as void as follows:

import org.testng.Assert;
import org.testng.annotations.Test;

public class Q45191867_Assert_Pass_Suite 
{

    @Test   
    public void TC002_CheckAssert() 
    {
        System.out.println("ajkcbh");
        try
        {
            Assert.assertEquals("Pass", "Pass");
        }
        catch(Exception e)
        {
            System.out.println("Exception:"+e.getLocalizedMessage());
        }
    }
}

The code block executes successfully when executed as TestNG Test.

I have executed the code block converting into TestNG with the following testng.xml as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Test">
    <classes>
      <class name="demo.Q45191867_Assert_Pass_Suite"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

I have executed this code block again as TestNG Suite. In this case as well the output on the console was:

[TestNG] Running:
  C:\Users\AtechM_03\LearnAutmation\LearnAutomationTestNG\testng.xml

ajkcbh

===============================================
Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

Let me know if this Answers your Question.



来源:https://stackoverflow.com/questions/45191867/testng-shows-0-test-run

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!