Author: Haroon Khalil

  • Patterns and best practices

    You probably noticed that the amount of code required to get our first system test working was much greater than. In this section, I introduce some patterns and best practices that will help you write maintainable web tests. These patterns come from my own experience after writing many such tests. Together with Guerra and Gerosa,…

  • Designing page objects

    For web applications and system testing, we do not want to exercise just one unit of the system but the entire system. We want to do what we called system testing. What should we test in a web application, with all the components working together and an infinite number of different paths to test? Following what…

  • An introduction to Selenium

    Before diving into the best practices, let’s get familiar with the mechanics of writing such tests. For that, we will rely on Selenium. The Selenium framework is a well-known tool that supports developers in testing web applications. Selenium can connect to any browser and control it. Then, through the Selenium API, we can give commands…

  • System tests

    At some point, your classes, business rules, persistence layers, and so on are combined to form, for example, a web application. Let’s think about how a web application traditionally works. Users visit a web page (that is, their browser makes a request to the server, and the server processes the request and returns a response…

  • Setting up infrastructure for SQL tests

    In our example, it was simple to open a connection, reset the database state, and so on, but that may become more complicated (or lengthy) when your database schema is complicated. Invest in test infrastructure to facilitate your SQL testing and make sure that when a developer wants to write an integration test, they do…

  • Writing automated tests for SQL queries

    We can use JUnit to write SQL tests. All we need to do is (1) establish a connection with the database, (2) make sure the database is in the right initial state, (3) execute the SQL query, and (4) check the output. Consider the following scenario: A simple JDBC implementation of such a class is…

  • What to test in a SQL query

    SQL is a robust language and contains many different functions we can use. Let’s simplify and look at queries as a composition of predicates. Here are some examples: In these examples, value < 50, i.customer_id = c.id, c.country = ‘NL’, and value > 50 and value < 200 are the predicates that compose the different queries. As a tester, a possible criterion is to exercise the predicates and check whether the SQL…

  • Database and SQL testing

    In many of the examples a Data Access Object (DAO) class is responsible for retrieving or persisting information in the database. Whenever these classes appear, we quickly stub or mock them out of our way. However, at some point, you need to test these classes. These DAOs often perform complex SQL queries, and they encapsulate a…

  • Testing larger components that go beyond our code base

    In the previous example, the large test gives us confidence about the overall behavior of the component, but we could still test each unit individually. In some cases, however, we cannot write tests for units in isolation. Or rather, we can write tests, but doing so would not make sense. Let’s look at examples of…

  • Testing larger components

    As always, let’s use a concrete example. Suppose we have the following requirement: Given a shopping cart with items, quantities, and respective unit prices, the final price of the cart is calculated as follows: NOTE The business rule related to delivery costs is not realistic. As a developer, when you notice such inconsistencies, you should talk…