• 用例设计

    测试类

    package TestXunit;

    import Data.buysData;
    import DemoXunit.Login;
    import DemoXunit.Products;
    import DemoXunit.Shopping;
    import org.testng.Assert;
    import org.testng.annotations.AfterClass;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.Test;

    public class BuysTest {

    Shopping shopping = new Shopping();
    Login login = new Login();
    Products pro1 = Products.getPro(1);
    Products pro2 = Products.getPro(2);
    Products pro3 = Products.getPro(3);

    /**
    * 每次测试方法前初始化库存
    *
    */

    @BeforeMethod
    public void resetCount1(){
    pro1.setCount(0);
    pro2.setCount(10);
    pro3.setCount(1);
    }

    /**
    * 获取每组数据(用例数据),执行用例
    *
    * @param name
    * @param pwd
    * @param porId
    * @param count
    * @param expect
    */

    @Test(dataProvider = "buysParams", dataProviderClass = buysData.class)
    public void testBuys(String name, String pwd, int porId, int count, int expect){
    login.userLogin(name, pwd);
    int result = shopping.buys(porId, count);
    Assert.assertEquals(result, expect);
    }

    /**
    * 测试类结束后退出登陆
    *
    */

    @AfterClass
    public void resetCount2(){
    login.userLogin("","");
    }
    }

    数据驱动

    package Data;

    import org.testng.annotations.DataProvider;

    public class buysData {

    @DataProvider
    public Object[][] buysParams(){
    return new Object[][]{
    {"","",1,1,-2}, //未登陆状态
    {"admin","123456",1,0,-1}, //管理员,购买数量为0
    {"admin","123456",1,-1,-1}, //管理员,购买数量为-1
    {"admin","123456",1,1,0}, //管理员,购买数量>0,且库存不足
    {"admin","123456",3,1,1}, //管理员,购买数量>0,且等于库存
    {"admin","123456",2,1,1}, //管理员,购买数量>0,且大于库存
    {"zhangsan","123456",1,0,-1}, //普通用户,购买数量为0
    {"zhangsan","123456",1,-1,-1}, //普通用户,购买数量为-1
    {"zhangsan","123456",1,1,0}, //普通用户,购买数量>0,且库存不足
    {"zhangsan","123456",3,1,1}, //普通用户,购买数量>0,且等于库存
    {"zhangsan","123456",2,1,1}, //普通用户,购买数量>0,且大于库存
    };
    }
    }

    testng配置

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="All Test Suite">
    <test verbose="1" preserve-order="true" name="/Users/irischen/IdeaProjects/XunitDemo-master">
    <groups>
    <dependencies>
    <group name="group1" depends-on="">
    <classes>
    <class name="TestXunit.BuysTest">
    <methods>
    <include name="testBuys"></include>
    </methods>
    </class>
    </classes>
    </group>
    </dependencies>
    </groups>
    </test>
    </suite>

    运行截图

    测试报告

  • 已提交

  • 作业1

    提取系统state连接种类

    netstat -ntp | sed 1,2d | awk '{print $6}' | sort | uniq -c | sort

    截图如下:

    作业2

    找出testerhome任意精华帖的任意回复的点赞数

    #随机取一个精华帖
    topic_id=`curl -s https://testerhome.com | grep -b1 "fa fa-diamond" | grep -o 'href="/topics/[0-9]*"' | awk -F '/|"' '{print $4}' | sort -R | head -1`
    url="https://testerhome.com/topics/"$topic_id

    #获取该精华帖回复数
    count=`curl -s $url | grep "共收到" | awk -F '>|<' '{print $3}' | head -1`

    #打印所选精华帖回复数
    echo "精华帖 "$url" 共有"$count"条回复"

    #判断是该精华帖是否有回复
    if [ $count > 0 ]; then
    #随机取该贴的一个回复
    reply_id=`curl -s $url | grep 'class="reply"' | awk -F '"' '{print $6}' | sort -R | head -1`

    #获取该回复点赞数
    zan=`curl -s $url | grep -A 50 "$reply_id" | grep -o 'data-count="[0-9]*"' | awk -F '"' '{print $2}' | head -1`

    #打印所选回复点赞数
    echo "其中"$reply_id" 共有"$zan"个赞"
    else
    echo "该精华帖无回复,无法获取任意回复点赞数"
    fi

    截图如下: