51Testing软件测试论坛
标题:
用JUnit4进行单元测试代码编写
[打印本页]
作者:
司格特
时间:
2018-3-8 15:03
标题:
用JUnit4进行单元测试代码编写
JUnit4简介
Junit是一个可编写重复测试的简单框架,是基于Xunit架构的单元测试框架的实例。JUnit4是JUnit框架
有史以来的最大改进,其主要目标便是利用JDK 5的Annotation特性简化测试用例的编写。JDK 5里可
以静态导入,例如import static org.junit.Assert.*;。Annotation又称注解(或元数据),其实就是@Be
fore、@After、@BeforeClass、@AfterClass、@Test等等。
@Before @BeforeClass @After @AfterClass的区别
简单来说,@Before@After会在每一个被@Test注解的方法前、后分别执行,比如有5个被@Test注解
的方法,那么@Before@After会被执行5次。被@Before@After注解的方法一般起到初始化和释放的作
用。这么做就保证了每一次@Test执行,他们都是相互独立,互不干扰的。
而@BeforeClass@AfterClass从头至尾仅会执行一次。对于那些较耗资源的,建议采用这种方法。
@Test执行顺序
一般来说,被@Test注解的方法是随机顺序执行的,因为单元测试,本身它们就应该是互不干扰可以
独立运行的。
但是某些特殊情况下,想让它按照一定顺序执行的话,可以使用@FixMethodOrder(MethodSorters.N
AME_ASCENDING),放在JUnit测试类定义的外面,这样一来就会按照方法名称的升序来执行。此时命
好方法的名字,就可以轻松实现既定顺序执行。
更多关于JUnit4
JUnit4代码块
将JUnit4测试类放在被测类的同一package下,让它们处于同一层级,这样就不用import被测类了。
package pkg_Selector;
import org.junit.*;
import org.junit.runners.MethodSorters;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import static org.junit.Assert.*;
/**
* Created by Sophie on 15/7/6.
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SelectorTest {
private static Selector s;
private static WebDriver afterLogin;
private static WebDriver afterSwitch;
private static WebDriver aftermouseMove;
private static WebDriver afterSearch;
private static WebDriver afterSelect;
@BeforeClass
public static void setUp() throws Exception {
s = new Selector();
}
@AfterClass
public static void tearDown() throws Exception {
s.tearDown();
}
@Test
public void test1_login() throws Exception {
afterLogin = s.login();
assertEquals("挖财社区,国内最大的理财社区", afterLogin.getTitle());
}
@Test
public void test2_switchWindow() throws Exception {
afterSwitch = s.switchWindow();
assertEquals("理财规划_理财学院_挖财社区,国内最大的理财社区,爱理财的人都在这里",afterSwitch.getTitle());
}
@Test
public void test3_mouseMove() throws Exception {
aftermouseMove = s.mouseMove();
assertEquals("用户",aftermouseMove.findElement(By.cssSelector("a.showmenu.xg1")).getText());
}
@Test
public void test4_search() throws Exception {
afterSearch = s.search();
assertEquals("查找好友 - 挖财社区",afterSearch.getTitle());
}
@Test
public void test5_select() throws InterruptedException {
afterSelect = s.select();
assertEquals(true,afterSelect.getPageSource().contains("以下是查找到的用户列表(最多显示 100 个)"));
}
}
复制代码
JUnit4运行结果截图
5个test都是通过的:
[attach]111679[/attach]
被测代码块
package pkg_Selector;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* Created by Sophie on 15/7/6.
*/
public class Selector {
private WebDriver dr;
private String url;
private String account;
private String pwd;
public Selector() {
this.url = "https://www.wacai.com/user/user.action?url=http%3A%2F%2Fbbs.wacai.com%2Fportal.php";
//Scanner input = new Scanner(System.in);
//System.out.println("Please input the account");
this.account = "******";//input.next();
//System.out.println("Please input the password");
this.pwd = "******";//input.next();
this.dr = new FirefoxDriver();
this.dr.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
public WebDriver login( ) throws Exception {
//登录
dr.get(url);
dr.findElement(By.id("account")).clear();
dr.findElement(By.id("account")).sendKeys(account);
dr.findElement(By.id("pwd")).clear();
dr.findElement(By.id("pwd")).sendKeys(pwd);
dr.findElement(By.id("login-btn")).click();
return dr;
}
public WebDriver switchWindow() throws Exception {
//打开理财规划子版块
String a = dr.findElement(By.linkText("理财规划")).getAttribute("href");
//直接用WebDriver.get()方式打开页面,浏览器不会新开页面,省得切换窗口
dr.get(a);
dr.manage().window().maximize();
//然后关闭左侧导航栏
dr.findElement(By.cssSelector("a.comiis_left_closes")).click();
return dr;
}
public WebDriver mouseMove() throws Exception {
//点开搜索类型下拉框,将鼠标移动到用户上并选择
Actions act = new Actions(dr);
WebElement dropDown = dr.findElement(By.cssSelector("a.showmenu.xg1"));
WebElement user = dr.findElement(By.cssSelector("ul#comiis_twtsc_type_menu>li>a[rel='user']"));
act.click(dropDown).perform();
act.moveToElement(user).click().perform();
act.moveByOffset(20, 30).click().perform();
return dr;
}
public WebDriver search() throws Exception {
//找到搜索输入框,并输入关键字,然后点击搜索按钮
WebElement input = dr.findElement(By.id("comiis_twtsc_txt"));
input.clear();
input.sendKeys("周杰伦");
dr.findElement(By.id("comiis_twtsc_btn")).click();//Click之后FireFox新开了一个页面
//用WindowHandle+页面title来切换dr至我们想要的窗口
String currentWindow = dr.getWindowHandle();
Set<String> handles = dr.getWindowHandles();
//System.out.println(handles.size());
Iterator h = handles.iterator();
while (h.hasNext()) {
dr = dr.switchTo().window((String) h.next());
//System.out.println(dr.getTitle());
if (dr.getTitle().equals("查找好友 - 挖财社区"))
break;
}
return dr;
}
public WebDriver select() throws InterruptedException {
dr.findElement(By.cssSelector("html>body#nv_home.pg_spacecp.sour>div#wp.wp.cl>div#ct.ct2_a.wp.cl>div.mn>div.bm.bw0>ul.tb.cl>li:nth-child(2)>a")).click();
/*用普通定位+click方式处理下拉框
dr.findElement(By.xpath("//select[@id='resideprovince']/option[@value='浙江省']")).click();
Thread.sleep(2000);
dr.findElement(By.xpath("//select[@id='residecity']/option[@value='杭州市']")).click();
*/
//用Select方式
Select sProvince = new Select(dr.findElement(By.xpath("//select[@id='resideprovince']")));
sProvince.selectByValue("浙江省");
Thread.sleep(2000);
Select sCity = new Select(dr.findElement(By.xpath("//select[@id='residecity']")));
sCity.selectByValue("杭州市");
dr.findElement(By.cssSelector("button.pn")).click();
return dr;
}
public void tearDown() throws Exception {
dr.quit();
}
public static void main(String[] args) throws Exception {
Selector s = new Selector();
s.login();
s.switchWindow();
s.mouseMove();
s.search();
WebDriver selectPage = s.select();
//xpath模糊查找
List<WebElement> searchResult = selectPage.findElements(By.cssSelector("li.bbda.cl"));
System.out.println("搜索到" + searchResult.size() + "个同城用户,他们是:");
System.out.printf("%-20s %-20s %-20s\n", "用户名", "用户等级", "用户积分");
String userName;
String userLevel;
String userMark = "";
String[] sArray;
for (int i = 0; i < searchResult.size(); i++) {
sArray = searchResult.get(i).findElement(By.cssSelector("li>p")).getText().split(" ");
if (sArray.length < 2)
userMark = "0";
else
userMark = sArray[3];
userLevel = sArray[0];
userName = searchResult.get(i).findElement(By.cssSelector("h4>a")).getText();
System.out.printf("%-20s %-20s %-20s\n",userName,userLevel,userMark);
s.tearDown();
}
}
}
复制代码
作者:
jingzizx
时间:
2018-3-8 16:03
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2