Introduction To Automated Testing With SimpleTest

Video loading...

  • 0:05
    Automated Testing in Drupal 7 with SimpleTest
  • 0:07
    Introduction to Automated Testing in Drupal 7 with SimpleTest
  • 0:12
    with Joe Shindelar
  • 0:17
    Hi!
  • 0:18
    Welcome to the Drupalize.Me series on Automated
  • 0:21
    Testing With SimpleTests.
  • 0:24
    In this lesson, I'm going to give you a relatively quick overview
  • 0:27
    of what you can expect to learn throughout the course
  • 0:30
    of this series.
  • 0:32
    And it basically boils down to this, we're
  • 0:35
    going to be talking about how you can get robots or computers to do
  • 0:38
    the oftentimes boring and definitely mind numbing work of click testing
  • 0:43
    a web application in order to ensure that it
  • 0:46
    does what it says on the cover.
  • 0:48
    Testing and quality assurance are an extremely important part
  • 0:51
    of maintaining quality software, but it can be really tedious.
  • 0:55
    Automated testing is the process of writing code to reduce the number
  • 1:00
    of things that we need to do manually when testing our software,
  • 1:04
    which in turn can dramatically reduce the amount of time
  • 1:06
    it takes to perform QA.
  • 1:09
    There are a lot of different frameworks
  • 1:10
    out there for writing tests and each of them has their pros and cons.
  • 1:14
    Throughout this series on testing, we'll
  • 1:17
    be focusing on the testing framework that's included with Drupal 7.
  • 1:21
    The system that Drupal uses for writing and running tests
  • 1:24
    is known as SimpleTest.
  • 1:26
    It's an extension of a PHP library with the same name.
  • 1:30
    We'll kick things off with some discussion about what exactly
  • 1:33
    automated testing is and why it's important and useful,
  • 1:37
    as well as providing a high level overview of the pieces that make up
  • 1:40
    automated testing like functional tests, and unit tests,
  • 1:44
    and tools for automating running of your automated tests.
  • 1:48
    In fact, a lot of this series is about
  • 1:50
    learning to write code that can take the grunt work out
  • 1:53
    of your day to day development by reducing the number of things you
  • 1:57
    have to manually check and manually test along the way.
  • 2:02
    Now we've got a presentation that covers the basics of SimpleTest
  • 2:05
    and walks through the components of a SimpleTest test case
  • 2:08
    and how all of the bits and pieces of code
  • 2:10
    to make up a test fit together.
  • 2:13
    And although SimpleTests is included in core,
  • 2:15
    it's not enabled by default.
  • 2:17
    So we'll go through enabling the module,
  • 2:19
    understanding all of the different configuration options associated
  • 2:22
    with tests and running tests and with both the UI
  • 2:26
    and running tests through Drush to see what the results look
  • 2:29
    like for passing and failing tests and how we can use the information
  • 2:34
    in the results to do things like debug failing tests.
  • 2:38
    Once we've got testing enabled and we know how to run tests,
  • 2:41
    we'll write a super simple HelloWorld test to try and tie it
  • 2:44
    all together and practice, mostly showing off how SimpleTest goes
  • 2:47
    about discovering our test cases and making
  • 2:50
    them feel them available for execution.
  • 2:52
    Then we'll look at assertions, the tool that lets us determine
  • 2:56
    whether or not the provided answer passes the test and all
  • 3:00
    the different types of assertions that we can make in SimpleTest,
  • 3:03
    like is the value of this variable equal to one
  • 3:06
    or does the text field name title exist in the form on this page?
  • 3:11
    We'll also write a handful of example assertions,
  • 3:14
    so we can see what they look like in code.
  • 3:17
    And then we'll really dive in, and throughout four or five lessons,
  • 3:20
    we'll learn how to use the DrupalWebtestCase class and all
  • 3:24
    the helper that provides for doing simple things like telling
  • 3:27
    the internal browser to click a link,
  • 3:29
    filling out forms, and installing modules.
  • 3:32
    One of the biggest benefits to using SimpleTests
  • 3:34
    over any other framework for testing a Drupal site
  • 3:37
    is that SimpleTest has a lot of internal knowledge about how Drupal
  • 3:40
    works, and we'll learn how to manipulate this,
  • 3:43
    so we can create and login as users with permissions
  • 3:46
    to do Admin-only tasks and to create dummy content,
  • 3:49
    we can use for testing.
  • 3:52
    After that, we'll look at some of the more complex tasks
  • 3:55
    that we might need to do in a test, like uploading files by a form
  • 3:58
    or triggering Ajax events on a page.
  • 4:02
    Although most of this series will be focused
  • 4:04
    on writing functional tests, we've got a whole lesson dedicated
  • 4:07
    to writing unit tests with SimpleTests, which will also cover
  • 4:12
    some of the pros and cons of unit tests.
  • 4:15
    Finally, we'll wrap up this series by looking at the 2x version
  • 4:19
    of the SimpleTest module and its ability
  • 4:21
    to run our tests in an environment that is a clone of a finished
  • 4:25
    website rather than the standard mechanism, which runs all
  • 4:28
    of our tests in a clean, fresh Drupal installation,
  • 4:32
    allowing us to use simple tests in all of its tools to write tests
  • 4:35
    against our production applications, to verify that they are working
  • 4:39
    just like we do for individual modules.
  • 4:42
    Once you've completed this series, you
  • 4:44
    should have a firm grasp on the concepts and practice of automated
  • 4:47
    testing and be able to implement them
  • 4:50
    using the Drupal 7 SimpleTest module.
  • 4:53
    And once you've completed writing tests
  • 4:55
    for your modules and your application,
  • 4:58
    you should be able to sleep better at night,
  • 5:00
    knowing everything is working and stable.
  • 5:03
    I hope you learn a bunch about testing in this series,
  • 5:05
    and I honestly believe that knowing how to write and run tests
  • 5:10
    will make you a better developer, overall.

Introduction to Automated Testing with SimpleTest

Loading...

Testing, and quality assurance, are an important part of maintaining high-quality software. Together they ensure that the code we write functions as it is intended to, and that changes made later on do not introduce any regressions into our application. Its tedious work and generally involves coming up with a list of tasks that prove a specific feature is working, and then repeating the list of tasks every time a change is made.

Automated testing is the process of writing code to reduce the number of things we need to do manually when testing our software. That list you created to ensure your software is working correctly is likely something that can be easily automated with a testing framework like SimpleTest. Doing so can dramatically reduce the amount of time it takes to perform QA, and improve the overall stability of our applications.

The SimpleTest testing framework was introduced into Drupal core early on in the Drupal 7 development cycle. Since then it has had a profound impact on the way our community develops Drupal. Core now contains a whole suite of tests that cover somewhere in the neighborhood of 75% of the softwares functionality. Every time a new features is introduced, or a bug is fixed, this battery of tests confirms that the fix to the date formatting function didn't inadvertently break something else.

In this series we'll learn to write our own automated tests for Drupal 7 using SimpleTests. We'll walk through enabling SimpleTest, and running one or more test cases with both the SimpleTest UI, and with drush. Then we'll cover some of the related terminology like the difference between functional tests and unit tests. As well as discuss why testing is an important part of development and worth the investment.

Then we'll walk through writing a test case and a set of assertions in orders to verify that various features of our site are working. We'll use functional tests based on the DrupalWebTestCase class, which simulates a browser navigating our site and clicking on links, and filling out forms. And the DrupalUnitTestCase class, which allows us to write super fast unit tests for the business logic contained within our code.

After watching this series you should be able to:

  • Install the SimpleTest module and run the tests included with core.
  • Define and know the difference between functional tests, unit tests, test cases, and assertions.
  • Create a new test case so that SimpleTest can discover and run your tests.
  • Write assertions that test the validity of your application in a given state.
  • Use the test browser to click on links and navigate to pages on a site.
  • Fill out and submit forms and AJAX form elements using the test case browser.
  • Handle file and image uploads in test cases.
  • Write unit tests, and understand how the workflow for running unit tests differs from that of functional tests.
  • Use the SimpleTest 7.x-2.x module from contrib to test existing websites.

This series assumes that you have basic understanding of Object Oriented PHP, and are familiar with Drupal module development. If you need to brush up on your module development watch our series on Drupal 7 Module Development. And, while not required to run tests, we do make use of drush in this series so knowing how to run drush commands is helpful.

Writing tests can be a challenging. But it doesn't have to be. And the benefits of doing so are huge. Especially as our web applications become more complex, as our teams grow, and as the multitude of ways in which people interact with our web applications changes constantly. Spending some time learning how to write tests will not only make your applications more reliable, it'll also make you a better developer.