{"id":801,"date":"2023-02-28T11:36:05","date_gmt":"2023-02-28T03:36:05","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=801"},"modified":"2023-04-05T17:42:22","modified_gmt":"2023-04-05T09:42:22","slug":"testng-write-test","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/28\/testng-write-test\/","title":{"rendered":"TestNG\u7f16\u5199\u6d4b\u8bd5"},"content":{"rendered":"<p>\u7f16\u5199TestNG\u6d4b\u8bd5\u57fa\u672c\u4e0a\u5305\u62ec\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ul>\n<li>\u6d4b\u8bd5\u4ee3\u7801\u6dfb\u52a0TestNG\u7684\u6ce8\u89e3<\/li>\n<li>\u6dfb\u52a0testng.xml\u6587\u4ef6<\/li>\n<li>\u8fd0\u884cTestNG<\/li>\n<\/ul>\n<p><!-- more --><\/p>\n<h2>TestNG\u6d4b\u8bd5\u6d41\u7a0b<\/h2>\n<p>\u901a\u8fc7\u4e00\u4e2a\u5b8c\u6574\u7684\u4f8b\u5b50\uff0c\u5b9e\u73b0TestNG\u6d4b\u8bd5\u6d41\u7a0b<\/p>\n<p>POJO\u7c7b\uff1aEmployee.java<\/p>\n<pre><code class=\"language-java\">package me.yezhou;\n\npublic class Employee {\n    private String name;\n    private double monthlySalary;\n    private int age;\n\n    public String getName() {\n        return name;\n    }\n\n    public void setName(String name) {\n        this.name = name;\n    }\n\n    public double getMonthlySalary() {\n        return monthlySalary;\n    }\n\n    public void setMonthlySalary(double monthlySalary) {\n        this.monthlySalary = monthlySalary;\n    }\n\n    public int getAge() {\n        return age;\n    }\n\n    public void setAge(int age) {\n        this.age = age;\n    }\n}<\/code><\/pre>\n<p>\u4e1a\u52a1\u7c7b\uff1aEmployeeBusiness.java<\/p>\n<pre><code class=\"language-java\">package me.yezhou;\n\npublic class EmployeeBusiness {\n    \/\/ Calculate the yearly salary of employee\n    public double calculateYearlySalary(Employee employee) {\n        double yearlySalary = 0;\n        yearlySalary = employee.getMonthlySalary() * 12;\n        return yearlySalary;\n    }\n\n    \/\/ Calculate the appraisal amount of employee\n    public double calculateAppraisal(Employee employee) {\n        double appraisal = 0;\n        if (employee.getMonthlySalary() &lt; 10000) {\n            appraisal = 500;\n        } else {\n            appraisal = 1000;\n        }\n        return appraisal;\n    }\n}<\/code><\/pre>\n<p>\u521b\u5efaTestNG\u6d4b\u8bd5\u7c7b\uff1aEmployeeTest.java<\/p>\n<pre><code class=\"language-java\">package me.yezhou;\n\nimport org.testng.Assert;\nimport org.testng.annotations.Test;\n\npublic class EmployeeTest {\n    Employee employee = new Employee();\n    EmployeeBusiness employeeBusiness = new EmployeeBusiness();\n\n    \/\/ test to check appraisal amount\n    @Test\n    public void testCalculateAppriasal() {\n        employee.setName(&quot;Joe.Ye&quot;);\n        employee.setAge(25);\n        employee.setMonthlySalary(8000);\n        double appraisal = employeeBusiness.calculateAppraisal(employee);\n        Assert.assertEquals(500.0, appraisal);\n    }\n\n    \/\/ test to check yearly salary\n    @Test\n    public void testCalculateYearlySalary() {\n        employee.setName(&quot;Joe.Ye&quot;);\n        employee.setAge(25);\n        employee.setMonthlySalary(8000);\n        double salary = employeeBusiness.calculateYearlySalary(employee);\n        Assert.assertEquals(96000.0, salary);\n    }\n}<\/code><\/pre>\n<p>\u521b\u5efaTestNG\u6d4b\u8bd5\u914d\u7f6e\u6587\u4ef6\uff1atestng.xml<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&lt;!DOCTYPE suite SYSTEM &quot;http:\/\/testng.org\/testng-1.0.dtd&quot; &gt;\n&lt;suite name=&quot;Suite&quot;&gt;\n    &lt;test name=&quot;test&quot;&gt;\n        &lt;classes&gt;\n            &lt;class name=&quot;me.yezhou.EmployeeTest&quot;\/&gt;\n        &lt;\/classes&gt;\n    &lt;\/test&gt;\n&lt;\/suite&gt;<\/code><\/pre>\n<ul>\n<li>\n<suite>\u6807\u7b7e\u4ee3\u8868\u4e00\u4e2a\u6d4b\u8bd5\u5957\u4ef6\uff0c\u53ef\u4ee5\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a\u6d4b\u8bd5\n<\/li>\n<li>\n<test>\u6807\u7b7e\u4ee3\u8868\u4e00\u4e2a\u6d4b\u8bd5\uff0c\u53ef\u4ee5\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2aTestNG\u6d4b\u8bd5\u7c7b\n<\/li>\n<li>\n<class>\u6807\u7b7e\u4ee3\u8868\u4e00\u4e2aTestNG\u7c7b\u662f\u4e00\u4e2aJava\u7c7b\uff0c\u5305\u542b\u81f3\u5c11\u4e00\u4e2aTestNG\u7684\u6ce8\u89e3\uff0c\u53ef\u4ee5\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a\u6d4b\u8bd5\u65b9\u6cd5\n<\/li>\n<\/ul>\n<h2>\u8fd0\u884c\u6d4b\u8bd5<\/h2>\n<p>IntelliJ IDEA \u8fd0\u884c\uff1aRun D:\/AutoTest\/AutoTest\/employee.xml<\/p>\n<p>\u547d\u4ee4\u884c\u8fd0\u884c\uff1ajavac &amp; java<\/p>\n<pre><code># javac Employee.java EmployeeBusiness.java EmployeeTest.java\n# java -cp &quot;C:\\TestNG_WORKSPACE&quot; org.testng.TestNG testng.xml<\/code><\/pre>\n<h2>\u6d4b\u8bd5\u62a5\u544a<\/h2>\n<p>TestNG\u6d4b\u8bd5\u5b8c\u6210\u4f1a\u5728test-output\u76ee\u5f55\u4e0b\u81ea\u52a8\u751f\u6210\u6d4b\u8bd5\u62a5\u544a\u3002\u4f7f\u7528\u6d4f\u89c8\u5668\u6253\u5f00index.html\uff0c\u9875\u9762\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6d4b\u8bd5\/TestNG\u6d4b\u8bd5\u62a5\u544a.png\" alt=\"TestNG\u6d4b\u8bd5\u62a5\u544a\" \/><\/p>\n<blockquote>\n<p>\u6ce8\u610f\uff1aIntelliJ IDEA\u81ea\u52a8\u751f\u6210testNG\u7684\u6d4b\u8bd5\u62a5\u544a\u9700\u8981\u5728\u8fd0\u884c\u914d\u7f6e\u4e0b\u62c9\u5c55\u5f00\u9009\u62e9Edit Configuration\uff0c\u627e\u5230Listeners\u52fe\u9009use default reporters\u5373\u53ef<\/p>\n<\/blockquote>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6d4b\u8bd5\/IDEA\u751f\u6210\u62a5\u544a.png\" alt=\"IDEA\u751f\u6210\u62a5\u544a\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7f16\u5199TestNG\u6d4b\u8bd5\u57fa\u672c\u4e0a\u5305\u62ec\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u6d4b\u8bd5\u4ee3\u7801\u6dfb\u52a0TestNG\u7684\u6ce8\u89e3 \u6dfb\u52a0testng.xml\u6587\u4ef6 \u8fd0\u884cT [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[224],"tags":[],"class_list":["post-801","post","type-post","status-publish","format-standard","hentry","category-testng"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/801","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=801"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/801\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}