Sometimes, as a test engineer, you may have to test in production, no matter how controversial this idea could be.

Here I will only consider «testing in production» in relation to autotests, and only if you have no other choice and no other fully functional environment is available.

Author’s photo of a server racks

Author’s photo of a server racks

Testing in production = field testing

Firstly, as a practitioner test engineer, I used to use the term «testing in production». But when I started studying this topic in computer science papers, I found it really hard to find anything relevant. It turned out that «testing in production» in CS papers and articles is called a «field testing» or «field-ready testing» (by the way, in ISTQB it is called the same). However, the term encompasses a broad range of testing activities:

  • Field testing indicates techniques that test systems in production.
  • Field testing exploits the end-user environment as a test ground for executing test cases throughout the application’s lifespan.

Thus, «field testing» implies much more than simply running automated tests, and the topic of the current article is limited only to examining the execution of automated tests in a production environment.

Testing environments

During the process of developing complex web applications, you may encounter the following environments:

  • Local / dev env — when an application runs on a local machine.
  • Pr / beta / pre-testing / staging env — when an application runs on a server, often with limited resources and functionality, and the app version is based on a git branch corresponding to a pull request.
  • Testing / test env — when an application runs on a server with test data. This is the main environment for QA engineers to work with and where regression testing is performed before release.
  • Pre-production / pre-stable env — when an application runs on a server with production data, but the app version differs from the production version.
  • Production / prod / stable env — when an application runs on a server to which end users have access.

Depending on a company’s resources and development processes, the number of environments, their types and naming may vary, but they are often similar to the list above.

An example of the environmental steps involved in deploying code to production.

Fig. 1. An example of the environmental steps involved in deploying code to production.

In a general sense, all types of environments except production, are test-related. Meanwhile, the local environment is unique because it is a developer’s local machine rather than a cloud-based server, and it provides an opportunity to run and test an app prior to the CI/CD cycle.

Reasons for absence of testing environments

Test-related environments are an essential part of the development and testing process. They are primarily used for running integration and end-to-end autotests, particularly acceptance and regression tests. However, these environments may be absent for a variety of reasons:

  • The absence of testing environments may be a feature of the application’s architecture (due to poor design or made intentionally).
  • It could also be impractical to keep testing environments due to the numerous variations in checks, making it impossible to perform even the minimum required number of tests during regression.
  • Third-party dependencies and plug-ins can make it inconvenient or even impossible to run end-to-end autotests in testing environments.
  • A fundamental difference in configuration between testing and production environments could mean that autotests that pass in testing cannot guarantee the functionality of the application in production.
  • The cost of maintaining testing environments may lead to them being refused due to the financial burden, or they may not be kept full-time operational.
  • Data privacy, security and compliance barriers may significantly limit the capabilities of testing environments for highly regulated systems.
  • Ongoing incidents on the internal infrastructure could result in the temporary or long-term unavailability of the testing environments.
  • At last, there may be periodic maintenance periods during which the testing environments may not be available.

The main reasons for lack of testing environments.

Fig. 2. The main reasons for lack of testing environments.

Of all these options, my case will relate to the last two: incidents and maintenance. This applies when your test architecture is intended for running automated tests in testing environments and you already have end-to-end autotests (API or UI), but encounter limitations in running them due to a lack of testing environments. In this case, you need to adjust and run these autotests in production, i.e. perform testing in production, or in scientific terms, conduct field testing.

Before proceeding to testing in production

Testing in production changes the whole idea of testing activity: from «we will not break» before release to «we did not break» after release.

Therefore, before heading to testing in production, it is better to think about shifting testing left and try to perform testing at the earliest stage of the software lifecycle. This means trying to run autotests in a local environment as much as possible. Can end-to-end autotests be run on a local developer’s machine? Can enough checks be performed for acceptance/regression testing in complete isolation? Perhaps you do not even need to run autotests in production.

Finally, if you decide to start testing in production, you must start to treat your autotests’ code as a real production code of the main application. Autotests are often treated as second-rate code, where developers can cut corners and quickly discard it as unnecessary. However, autotests for production are no longer considered such. They are the same production code that runs above real-life business flows. All development practices and requirements that apply to the application’s code must also apply to its autotests, including code review, static analysis and versioning, as well as autotests for autotests.

Autotests in production should be carried alongside monitoring and alerts in the production environment (this is an SRE’s responsibility). Autotests may reveal errors before they lead to system failures, and these errors will be visible on monitoring dashboards, but production testing is not a replacement for common observability practices.

Things to consider about

  • First of all, access to the infrastructure of the production environment. For security reasons, this access should not be permanent. It should be limited to a maximum of 30, 90, 180 or 360 days. Access should not be revoked on Fridays or on 31 December. People usually forget about the end of the access period, so a notification system would be good.
  • The ability to run autotests in production should also be limited. QAs, on-call developers and stakeholders are enough, but definitely not everyone in the whole department.
  • Any sensitive test data used in production testing, like passwords or payment data, must be stored in CI’s «secrets». It should never be transferred openly in environment variables or code.
  • All secrets and sensitive data must be masked in all logs and test reports. Unfortunately, it complicates debugging, but this is production data, not a test one anymore.
  • For HTTP API tests, the unique randomly generated request ID must be passed as X-Request-ID header.
    • If it is possible, you can embed not only an indication that this request came from automated tests, but also an indication of the test itself in the request ID, e.g. e2e-api-test-123-wk9DYfVDjIKQtUv2a0G2h. This would not just be a test request — it would be a test request with a clear, instant indication of where it came from and who initiated it.
  • When running tests after release, even if the release can be deployed on production at any time, the timing of the tests must align with that of the business processes.
    • Another option of running worth considering is cost-effective scheduling.
  • Ask your security department to review the architecture of your test framework. You may be surprised by how many issues there are in your autotests.
  • Even if the autotest architecture and process have been reviewed by the security department, it is still unclear what external auditors may say about testing real money payments in production.
    • Another case of testing real money payments, there should be a limit on transactions involving a third party, such as a bank or payment system, to which neither your system nor the autotests have access. This is one of the ways to prevent the budget from being exceeded.
  • When selecting test cases for production, bear in mind that if something unpredictable occurs, it cannot be stopped or undone.
    • Test cases should guarantee that testing the application in production does not have any perceivable effects on real users.
  • You need to prepare a working protocol in advance, detailing what to do if an error occurs in production during testing. This protocol should be included in the documentation or test description, or even in comments in the code, so that all testers and developers can find it and know how to fix issues caused by tests.
  • Watch out for pre-conditions and post-post conditions. All data inputs must be pre-checked for valid testing data, non-null and non-undefined values. Statically typed programming languages and runtime schema validation tools are highly recommended here.
  • You must not inject or delete anything directly to/from the database. In general, you must only interact with the application through its public API.
  • Maximise the number of test assertions in production. Do not check only general parameters for certain values like type, non-null or above 0. Check precise values. This will make your tests more fragile, but it will also make you perform checks in production more consciously.

Testing in production is a controversial practice. It involves a trade-off between accepting risks and maintaining quality. Therefore, the decision to run autotests in production should be made by the whole team, not just QAs or stakeholders.

Copy @ Medium