Data Management approaches for your tests
July 30, 20203 Technical Testing Books to improve your test tech skills
August 13, 2020Introduction
Although extremely important, we are not talking here about unit testing and integration testing, assuming you already know what it is and already apply it, as a developer concerned about the quality of your code.
Test Automation Pyramid and Agile Testing Quadrants
Creating tests is the easiest and quickest way to test your application and ensure that if any bug appears, you will find it before your client.
Currently, we cannot talk about testing without talking about test automation. The application of the test automation gives quick feedback to the team and maintains the execution of the regression tests continuously.
The approach we can use to take a greater speed in automating, executing, and having rapid feedback on the tests is the application of the Test Pyramid, which is a guide for the application of automation in at least three levels: unit, services, and ui (user interface).
The services layer is divided into three parts: component tests, integration tests, and API tests.
The unit testing layer is the most important layer of our application because we will create tests for the code that we are developing and guarantee it works as expected, even after future maintenance (recommending the use of TDD — Test Driven Development). In this layer, we can apply code coverage analysis and static analysis practices to intensify the rapid feedback against a defect that may appear.
The service layer (component, integration, and API) is extremely important nowadays, with a large focus on API testing. Here we apply mocks, stubs, and fakes to give speed in the execution of microservice tests. We also need separate test environment servers, where this can be the closest to a production environment.
The UI (User Interface) layer is also important, especially from a mobile testing perspective where, when a customer encounters an error in an app, it usually removes it. The most important techniques here are automated testing in UI and Visual Regression Testing. In the web part, we need browsers to execute the same test in different ones (IE, Chrome, Firefox, Safari). We need the same for mobile automation testing: iOS and Android devices to ensure the compatibility of our app on these two platforms.
The Agile Testing Quadrant, created by Brian Marick and widely disseminated by Lisa Crispin and Janet Gregory in his book “Agile Testing — A Practical Guide for Testers and Agile Teams” are some practices that can be applied during the activity-focused development of the test. The quadrant is a guide: it is not necessary to perform all the existing practices in it, you can choose one or more depending on your context.
Continuous Delivery and Testing Pipeline
There is no way to talk about DevOps without talking about Continuous Delivery (CD). Without it, we could not even talk about the DevOps culture.
In Continuous Delivery, one of the foundations is Continuous Testing where we must test all the stages of our development (pipeline) with an initial recommendation applied to unit tests and automated acceptance.
Continuous Delivery enables joint roles between Development, QA, and Operation. An example of evidence-focused collaboration for these roles is:
- Development + QA: Build, Deploy, and Test automation at various levels
- QA + Operations: Test Automation and Continuous Feedback through test executions, as well as sanity test executions
- Operations + Development: Automated provisioning of machines/ containers required for testing at any level.
Now we continue with the pipeline focused on tests, which can be applied in whole or in parts, being:
Unit Test and Integration Test
We can create mocks/fakes/stubs to remove the dependencies and accelerate the test execution.
ServiceTest: on service layers (SOAP, REST)
- Smoke: small execution subset to guarantee that all APIs are working (at least return status different from HTTP 404 or HTTP 500)
- Contract: a collection of agreements (tests) between a client (consumer) and an API
- Functional: tests that want to guarantee the operation against different business rules (happy path, sad path, and alternative flows)
- Acceptance: evaluate the system’s compliance with the business requirements and assess whether it is acceptable for delivery
Acceptance: acceptance testing
Tests that focus on the user perspective and known as end-to-end testing (e2e). Important to simulate the user journey on the application.
- Smoke: main test suite that will guarantee your business running
Functional: functional testing
Tests that guarantee the operation against different business rules (happy path, sad/negative path, and alternative flows)
- Smoke: main test suites of a happy path, sad path, and alternative flows
Non-Functional tests
From Integration to Functional Testing (end of the pipeline), we have to worry about non-functional tests. Examples of tests are functional: performance, load, security, etc …
And it’s extremely necessary to create an entire automated test architecture to support the continuous, automated, and least-maintenance run possible with:
- screenshots to evidence the execution of each test, or evidence when an error occurs
- logs to analyze and see if any error occurred
- reports to show feedback about the test execution
- data management for the sensitive data on a test script
- parameterize commonly changed data like URLs, endpoints, etc…
Toolbox for Automated API, Web, and Mobile tests
To automate an API, a Web page, and a Mobile front-end, there are open-source tools that will help you to quickly and easily build and run tests.
Rest Assured
https://github.com/rest-assured/rest-assured
Tool for creating automated tests for an API (REST and XML). Uses an easily understood DSL based on Gherkin (Given-When-Then).
In the example below, it is possible to see the API through a local endpoint (simulating a production environment) and a mock endpoint created with Java Spark. Creating a mock API by developing the API fixed data returns can give even greater speed in the execution and validation of the different aspects that secure the tests for microservices, especially contract tests.
@Test
public void getPersonById() {
int personID =
given()
.contentType(ContentType.JSON)
.body(new Person("Elias Nogueira", "RS", "Automate tests")).
when().
post("person").
then().
extract().
path("id");
when().
get("person/{id}", personID).
then().
contentType("application/json").and().
body("name", equalTo("Elias Nogueira")).and().
body("address", equalTo("RS")).and().
body("hobbies", equalTo("Automate tests")).and().
statusCode(200);
}
Selenium WebDriver
It is the best-known tool for the automation of a web page. It also has an easy DSL and is based on four steps for automation:
- Navigation: actions like accessing a web page, forwarding, back, and refresh
- Interrogation: ways to find web elements like by id, name, cssSelectors, and other locators
- Manipulation: a way to interact with an element like click, fill (sendKeys), clear, and get text
- Synchronization: ways to wait for some asynchronous actions, like an element that appears after some seconds
It is a W3C standard and performs actions in web browsers simulating a real browser. For this to be possible it is necessary to use the browser’s drivers.
@Test
public void addPersonSuccessfully() {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, 10);
driver.get("http://localhost:8888/javaone");
By addButton = By.id("add");
wait.until(ExpectedConditions.presenceOfElementLocated(addButton));
driver.findElement(addButton).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("back")));
driver.findElement(By.id("name")).sendKeys("Daenerys Targaryen");
driver.findElement(By.name("address")).sendKeys("Dragonstone");
driver.findElement(By.cssSelector("input[ng-model='post.hobbies']")).sendKeys("Break Chains");
driver.findElement(By.cssSelector(".w3-btn.w3-teal")).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("address")));
String dataOnPage = driver.getPageSource();
assertTrue(dataOnPage.contains("Daenerys Targaryen"));
assertTrue(dataOnPage.contains("Dragonstone"));
assertTrue(dataOnPage.contains("Break Chains"));
driver.quit();
}
Appium
It is an open-source tool with the same Selenium DSL, but for automation in native or hybrid mobile device apps for iOS or Android.
It supports execution on emulators, real devices, or test labs (cloud), and, in conjunction with Selenium Grid, gives the possibility of creating an internal device grid.
@Test
public void addPerson_Successfully() throws MalformedURLException {
File app = new File("src/main/resources/app/workshop.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
capabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "com.eliasnogueira.workshop");
capabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, "activities.ListActivity");
AndroidDriver<MobileElement> driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("com.eliasnogueira.workshop:id/fab")));
driver.findElement(By.id("com.eliasnogueira.workshop:id/fab")).click();
driver.findElement(By.id("com.eliasnogueira.workshop:id/txt_nome")).sendKeys("Jon Snow");
driver.findElement(By.id("com.eliasnogueira.workshop:id/txt_endereco")).sendKeys("The wall");
driver.findElement(By.id("com.eliasnogueira.workshop:id/txt_hobbies")).sendKeys("Know nothing");
driver.findElement(By.id("com.eliasnogueira.workshop:id/button")).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("android:id/search_button")));
driver.findElement(By.id("android:id/search_button")).click();
driver.findElement(By.id("android:id/search_src_text")).sendKeys("Jon Snow");
String text = driver.findElement(By.id("android:id/text1")).getText();
assertEquals("Jon Snow", text);
driver.quit();
}
Complete example
The code for all projects can be found in the repository https://github.com/eliasnogueira/test-automation-javaone-2017
There are test suites for each pipeline step.