美文网首页
JUnit小结

JUnit小结

作者: 海的那一边 | 来源:发表于2018-08-31 15:15 被阅读21次

在appiumUI自动化中,使用到了junit,对junit做个小结。

1.junit包引入

eclipse内部集成了junit,我们只需要引入即可。如果没有引入junit,那么将不能进行junit test。


image.png image.png
image.png

这样引入后就可以使用junit了。我们的project中的junit在下面的这个包里面,所以没有再添加。

image.png

2.创建junit test case

创建引入了junit后,就可以创建junit test case了。


屏幕快照 2018-08-31 上午11.39.57.png
package com.xingshulin.base;

import java.net.URL;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import com.xingshulin.util.ReadConfigIos;
import io.appium.java_client.ios.IOSDriver;
import junit.framework.TestCase;

public abstract class BaseIosTestCase extends TestCase {
    IOSDriver<?> driver;

    @Before
    public void setUp() throws Exception {
        ReadConfigIos readConfigIos = new ReadConfigIos();
        DesiredCapabilities cap = new DesiredCapabilities();
        readConfigIos.getConfig();

        cap.setCapability("automationName", readConfigIos.AUTOMATIONNAME);// appium做自动化
        cap.setCapability("platformVersion", readConfigIos.PLATFORMVERSION);// 系统版本
        cap.setCapability("deviceName", readConfigIos.DEVICENAME);// 设备名称
        cap.setCapability("platformName", readConfigIos.PLATFORMNAME); // 安卓自动化还是IOS自动化
        cap.setCapability("udid", readConfigIos.UUID);// 设备的udid
        cap.setCapability("bundleId", readConfigIos.BUNDLEID);// 被测app的包名
        cap.setCapability("unicodeKeyboard", readConfigIos.UNICODEKEYBOARD); // 支持中文输入
        cap.setCapability("resetKeyboard", readConfigIos.RESETKEYBOARD); // 支持中文输入,必须两条都配置
        cap.setCapability("noSign", readConfigIos.NOSIGN); // 不重新签名apk
        cap.setCapability("newCommandTimeout", readConfigIos.NEWCOMMANDTIMEOUT); // 没有新命令,appium30秒退出
        cap.setCapability("recreateChromeDriverSessions", true);

        driver = new IOSDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap);

    }

    public IOSDriver<?> getDriver() {
        return driver;
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
    }

    @Test
    public abstract void test() throws Exception;

}

说明:

在junit4中,定义一个测试方法变得简单很多,只需要在方法前加上@Test就行了,测试方法test()必须以public void修饰,并且不包含参数。

进行JUnit测试时,有些配置需要初始化,这些在SetUp()中定义。使用注解@Before,它会在每个用例运行之前都运行一次。主要用于一些独立于用例之间的准备工作。

测试方法运行完之后,要进行一些扫尾的工作,这些在tearDown()中定义。@After这个注解表示这个方法会在每个测试方法执行之后执行一次。

Junit还有一些别的注解,如@Ignore、@BeforeClass、@AfterClass、@Runwith、@Parameters

3.测试套件

测试套件可以同时测试多个测试类。执行这个测试套件,就会执行里面添加的测试类。

package suite.push;

import org.apache.log4j.Logger;

import junit.framework.JUnit4TestAdapter;
import junit.framework.Test;
import junit.framework.TestSuite;
import testcase_ios.push.AgreeGroupApplyTestCase;
import testcase_ios.push.AuthenticationFailTestCase;
import testcase_ios.push.AuthenticationPassTestCase;
import testcase_ios.push.FeedbackTestCase;
import testcase_ios.push.GroupApplyTestCase;
import testcase_ios.push.GroupFinishTodoNotificationTestCase;
import testcase_ios.push.GroupNewConditionTestCase;
import testcase_ios.push.GroupReceiveTodoNotificationTestCase;
import testcase_ios.push.OpenAPPByIntentTypeTestCase;
import testcase_ios.push.OpenAPPByMsgTypeTestCase;
import testcase_ios.push.OpenAppTestCase;
import testcase_ios.push.OpenInnerURLTestCase;
import testcase_ios.push.OpenURLTestCase;
import testcase_ios.push.QuanDiscussPageByIntentTypeTestCase;
import testcase_ios.push.QuanDiscussPageByMsgTypeTestCase;
import testcase_ios.push.QuanGroupPageByIntentTypeTestCase;
import testcase_ios.push.QuanGroupPageByMsgTypeTestCase;
import testcase_ios.push.QuanHomePageByIntentTypeTestCase;
import testcase_ios.push.QuanHomePageByMsgTypeTestCase;
import testcase_ios.push.RemoveAffixTestCase;

public class SuiteIosPush extends TestSuite {

    public Logger log = Logger.getLogger(this.getClass().getName());

    public static Test suite() {
        TestSuite suite = new TestSuite("Suite Of IOS Push");
        suite.addTest(new JUnit4TestAdapter(OpenAppTestCase.class));
        suite.addTest(new JUnit4TestAdapter(OpenURLTestCase.class));
        suite.addTest(new JUnit4TestAdapter(FeedbackTestCase.class));
        suite.addTest(new JUnit4TestAdapter(GroupApplyTestCase.class));
        suite.addTest(new JUnit4TestAdapter(RemoveAffixTestCase.class));
        suite.addTest(new JUnit4TestAdapter(OpenInnerURLTestCase.class));
        suite.addTest(new JUnit4TestAdapter(AgreeGroupApplyTestCase.class));
        suite.addTest(new JUnit4TestAdapter(GroupNewConditionTestCase.class));
        suite.addTest(new JUnit4TestAdapter(OpenAPPByMsgTypeTestCase.class));
        suite.addTest(new JUnit4TestAdapter(OpenAPPByIntentTypeTestCase.class));
        suite.addTest(new JUnit4TestAdapter(QuanHomePageByMsgTypeTestCase.class));
        suite.addTest(new JUnit4TestAdapter(QuanGroupPageByMsgTypeTestCase.class));
        suite.addTest(new JUnit4TestAdapter(GroupFinishTodoNotificationTestCase.class));
        suite.addTest(new JUnit4TestAdapter(QuanDiscussPageByMsgTypeTestCase.class));
        suite.addTest(new JUnit4TestAdapter(QuanGroupPageByIntentTypeTestCase.class));
        suite.addTest(new JUnit4TestAdapter(QuanHomePageByIntentTypeTestCase.class));
        suite.addTest(new JUnit4TestAdapter(GroupReceiveTodoNotificationTestCase.class));
        suite.addTest(new JUnit4TestAdapter(QuanDiscussPageByIntentTypeTestCase.class));
        suite.addTest(new JUnit4TestAdapter(AuthenticationFailTestCase.class));
        suite.addTest(new JUnit4TestAdapter(AuthenticationPassTestCase.class));

        return suite;
    }

    public static void main(String[] args) {
        String filePath = args[0];
        FilePathAll.FILEPATH=filePath;
        junit.textui.TestRunner.run(SuiteIosPush.suite());
    }

}

说明:

几个概念:测试方法、测试类、测试集、测试运行器。
其中测试方法就是用@Test注解的一些函数。测试类是包含一个或多个测试方法的一个**Test.java文件,测试集是一个suite,可能包含多个测试类。

相关文章

网友评论

      本文标题:JUnit小结

      本文链接:https://www.haomeiwen.com/subject/zxkswftx.html