============== JUnit tutorial ============== This tutorial will give you a brief introduction to JUnit. You will be able to use what you learn in this tutorial for :ref:`exploratory`. You can find the preliminary tutorial slides here: :download:`Tutorial 1 <../files/T01-MBT-Tutorial-1.pdf>`. Preparation =========== Before the tutorial starts, you can already clone the project's git repository to your system and try some of the installation steps in the read.me file. The source code can be found `here `_. Writing tests ============= Here are some resources that will help you to familiarize yourself with JUnit: - `JUnit homepage`_ - `JUnit getting started page`_ .. _JUnit homepage: http://junit.org/ .. _JUnit getting started page: https://github.com/junit-team/junit/wiki/Getting-started Sample code =========== Any sample code used during the tutorial will be presented here, after the tutorial. .. .. highlight:: java .. .. Here is the code from the tutorial:: .. .. import static org.junit.Assert.*; .. .. import org.junit.Test; .. import org.junit.After; .. import org.junit.Before; .. .. import org.openqa.selenium.*; .. import org.openqa.selenium.firefox.FirefoxDriver; .. .. public class MyFirstTest { .. .. WebDriver driver; .. .. @Before .. public void goToFire() { .. driver = new FirefoxDriver(); .. driver.get("http://127.0.0.1:5000/lbs/"); .. } .. .. private void login(String username, String password) { .. driver.findElement(By.name("email")).sendKeys(username); .. driver.findElement(By.name("password")).sendKeys(password); .. driver.findElement(By.id("loginbtn")).click(); .. } .. .. @Test .. public void testFireLoginAndLogout() { .. login("arnarbi@gmail.com","test"); .. assertEquals("Laboration overview for Student Birgisson (TDA602)", .. driver.findElement(By.tagName("h1")).getText()); .. } .. .. @Test .. public void testFireBadLogin() { .. login("arnarbi@gmail.com", "testxxx"); .. assertEquals("Please log in", .. driver.findElement(By.tagName("h1")).getText()); .. } .. .. @After .. public void quitBrowser() { .. driver.quit(); .. } .. .. }