Home > Articles

Video: Continuous Delivery: Architecture and Process for Acceptance Tests

In this excerpt from Continuous Delivery LiveLessons (Video Training), Jez Humble talks about how to work with tests and test code.
From the Video

Transcript

The first principle in test automation is that tests are first class citizens of your project. This is important in a couple of ways. Firstly in terms of preventing decay in test code. We've already spoken about how important it is to treat your test code as if it were real production code. Test code is at least as hard to write and maintain and debug as production code, so you need to treat it with the same love and care and respect. That means refactoring relentlessly to make that your code is readable and expresses intent clearly and is maintainable. Don't repeat yourself. Anywhere you have code that is duplicated across tests find ways to remove that duplication, but again, not at the expense of readability. Of course, it's important not to repeat yourself. That is vital to creating well-maintained code.

Then, perhaps controversially, don't use record and playback tools to build your test suites. These kinds of tools you record clicking on the UI and then you play it back to run the test. The problem with those tools is twofold. Firstly it can tightly couple your tests to the way that the UI is organized. If you start moving UI elements or changing the layout of screens suddenly your test suites fail and then you have to go back and fix them. Then also as I mentioned earlier, many of these tools have licenses which prevent developers from using them. In general I've seen that using record and playback tools, unless you're very, very careful, those suites of tests are very painful and expensive to maintain and it's very hard for developers to be able to actually reason about them and reproduce them.

One idea that's been very important in the development of test automation is the idea of executable specifications. This is the idea that we can use a natural language to express the acceptance criteria for our system under test while using a general purpose programming language to express test mechanics. Using a general programming language is important because there are tools that help you actually maintain that code. Keep it refactored, you've got Ids which make it very easy to refactor your code base. We should be applying those tools to our test suites in order to keep them maintainable. Then use a tool that allows you to operate in either domain transparently.

There's a number of tools that let you operate in this way. There's Cucumber, Ghurkin, Concordia, these all open source tools and I'm going to show you a relatively new open source tool right now called Gauge. Gauge is a tool that lets you write scenarios in plain text in markdown format. What you can see here is a scenario I wrote just for this example. Then you write the code which actually drives the application under test in the language of your choice. Gauge has runners for Java, C sharp, Ruby. You can write your own runners. What Gauge lets you do is you run your commands on the command line and it executes these specifications.

What you can see here on screen is a specification for buying a book. There's an initial step which sets up a user with a balance of 39 dollars 99 in their account and then I have a scenario which is to buy a product. We're going to buy the continuous delivery book and make sure we only have zero dollars left in our account. When I run the tool what it's going to do is look for some codes that's actually going to execute these steps. This is some Java code I wrote which will do the real work and you can see that each method in this code has an annotation. The annotation matches the text in the scenario file. Actually creating the user will run this method that creates a new account manager and a new shopping cart and then buying the products will go and actually look for a product with the specified title and then buy it. Then validating at the end that the user has zero dollars left is going to actually execute an insertion to make sure there's no money left in the account.

This is very powerful. Testers and business people can collaborate on editing the scenario files without having to know anything about the code and the developers can focus on making sure the code works and does what it's supposed to do. We can actually execute these specifications quite literally. However there is an overhead to doing this. I need to make sure that the annotation text matches up with the text in the scenarios. As you change those scenarios you're going to need to makes sure that the text in the annotations is up to day. However, if I look at this test and expressed it just as a J unit method you'd see that that code is much shorter and more concise. I just got a method called buying products and debit account which just executes exactly the same code and it's shorter and it's easier to read.

The executable specifications methodology here, that has some real benefits. It means that if you have non-technical users who you're working with to define your specifications, you can work with them just using the text. That's a very powerful communication tool. If you do need to have your specifications clearly listed as part of your product delivery process or you're working to regulate a domain being able to take what's in the code and turn it into actual real specifications just in a programatic way is also very, very powerful. You really want to avoid the situation where the specifications are in a document that's not regularly updated and doesn't keep in sync the codes. It's very easy for those kind of documents to drift out of spec as we introduce changes to the code. This is a really nice solution to that problem. But, if you don't need any of those things, if you're just knocking up the quick products or if you don't have non-technical users who are developing your product or service then the overhead might not be worth it and it might just be worth using a simple tool like J unit in order to write your automated acceptance test.

One thing which is definitely essential whichever of these methods you use is to decouple the test script information here on what's actually happening with the driving of the system under test. Underneath both this layer and the layer in the executable specifications example we've got actually a product catalog cart which knows how to interact with the system under test by actually clicking on text boxes and buttons and reading back what happens in the browser. Her we can see that there's no interaction with the system under test in this layer of the code base. All we're doing is interacting with the main objects on the test side and feeding it information on the initial state and validating that the system is in the expected state at the end. All the interaction with the system under test is in this separate layer.

This is an example of an application of a pattern called the page object pattern. In the page object pattern we write a separate class for every page in the system that we want to interact with. This example is for a login page. We have a method to log in with the username and password. Then we have another method to log in where we're expecting an error where again we pass in the username and the password. The idea here is to make sure that if we make changes to the UI of the system under test we only need to change our test code in one place, right here. I don't need to go and update all the test scripts, specifications or the top level of test code where we actually go through the scenario. I just need to change the place in the code which interacts with those particular UI elements.

That's one very powerful benefit of the page object pattern. Another benefit is that it allows me to reuse the same test scripts and scenarios against multiple different implementations of the system. For example I could have a login page as you can see here, which interacts with a web front end via Selenium. I could write another implementation of this which interacts via a service layer over HTTP, not via the UI. By turning this into an interface and having multiple different implementations that allows me to use the same tests and REOs against both the UI layer or the service layer. I could even write an implementation which talks to say an app, iPhone app or Android app and reuse the same scenarios against multiple different devices. It's a pattern that also allows you to reuse test scripts against multiple different end points.

I'm going to change tack a bit and talk about the process behind implementing automated tests. When you come up with a new idea, story, requirement you'll start by describing what the value of that story is and also defining acceptance criteria, the statements by which you know that that work will be done. Those are typically written by customers, analysts, testers working together. However actually implementing the test codes is going to be done by developers and testers working together. In some traditional organizations all the test scripts are written and maintained by testers. This is very problematic because unless developers and testers work together it's very hard for developers to learn how to write maintainable, testable code. Actually having the developers see the system from the point of view of the tester, the person interacting with the system and helping to design the tests is really powerful in helping them build testable software.

If you write the software and then the testers come along afterwards and build the test suites it's often actually quite hard to build test suites against software that's not been designed to be testable. Having that feedback loop of developers and testers working together is really powerful in making sure the software's testable in order to make sure that the test suites are maintainable. Having the testers in a different room or a different building or in different country or even outsourced, my experience has been that that always reduces quality. It always makes for poor quality software. Having the testers and developers on the same team collaborating on developing the tests leads to much better outcomes because it means the software's more testable. It also means that the developers actually understand how to write maintainable test suites and that the test suites are better maintained and that the developers pay attention to them.

My recommendation is developers and testers should actually pair program on test implementation. This has the advantage that the testers don't need to understand test automation. One of the problems that I've seen before is testers are afraid of adopting test automation because it's a whole bunch of new skills they have to learn. If you paired them up with developers that's a great way for them to actually be able to work together and learn those new skills and still add a ton of value to the process. Developers also then learn a lot about what it is to effectively test your code. They also learn a lot about the user's viewpoint, which is what the tester provides in that process.

I want to talk a little bit more about the role of a tester or quality analyst. The tester is a role, it's not a person. You don't actually necessarily need people who are dedicated to testing. You can have developers play the testing role on other people's work typically because people are bad at testing their own work. That's a perfectly acceptable thing to do. Testers are not failed developers. This unfortunately is a very commonly held view, but when you consider that maintaining test code is as hard as maintaining a production code the idea that you should put somebody who's not as skilled on that job is crazy. Testers have a very unique set of skills. It's important that we treat them with the same respect as we treat developers.

The main job of a tester is to advocate for the user and to make the quality of the system transparent so the team can make effective decisions around their test automation and their test strategy. They're not primarily working on manual regression testing. This is a case where the computers are laughing at us if we're having human beings manually do regression against the system under test. That's something that should be automated. What they should be doing is focusing on exploratory testing and helping to build and maintain the suites of automated tests. That should be the main work of testers.

Takeaways from this section of the course. It's important to make sure that your acceptance tests are running before you can say that you're done with any functionality. Being able to say that you're done requires that you complete the tests. I'm not that religious about writing acceptance tests before you write the code. I've seen it work well either way. Either write the acceptance test before or afterwards, but the important thing is you should finish the acceptance test before you say you're done with the functionality. It's very important to use nice layered architecture for your test harness and make sure that you encapsulate interaction with the system under test using a pattern like the page object pattern. Those acceptance tests must be owned by and maintained by the whole team. They're not just the responsibility of the testers. Where that's true often the developers just don't pay attention to them and then they degrade and become very painful to maintain and often become of zero value.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020