Best practices for Cisco pyATS test scripts
Test scripts are the heart of any job in pyATS. Best practices for test scripts include proper structure, API integration and the use of Python libraries to enhance testing.
The average enterprise has thousands of devices running at any moment, which could overburden even the most well-staffed organization. It's almost impossible for any network team to monitor so much information. The sheer volume of data could make it difficult to successfully monitor such a complex environment. That's where network management tools, like Cisco's Python Automated Test System come in.
PyATS is a Python-based testing framework that enables network administrators to automate network devices, systems and testing activities to validate the status of operations. The tool signifies the shift from traditional network operations to NetOps, which combines network operations with DevOps practices. Cisco originally developed pyATS in 2014 for its in-house engineers and later released it to the general public in 2017 to address the need for automated network testing.
In their book, Cisco pyATS Network Test and Automation Solution: Data-driven and reusable testing for modern networks, co-authors John Capobianco, product marketing evangelist at Selector, and Dan Wade, practice lead of network and infrastructure automation at BlueAlly, provide an overview of pyATS and its features. PyATS offers additional capabilities beyond automated testing of a network. Capobianco and Wade underscore pyATS capabilities and use cases in their book.
"Testing is at the core of what pyATS does," Wade said in an interview with TechTarget Editorial. "Testing [with pyATS] has a very low risk. You don't have to worry about taking down a network if you want to run a pyATS job, and you learn a lot about your network."
In Chapter 4 of Cisco pyATS Network Test and Automation Solution, "AEtest Test Infrastructure," Capobianco and Wade cover a wide range of topics on Automation Easy Testing (AEtest) in pyATS.
Capobianco and Wade offered best practices for network engineers getting started with running test scripts for a pyATS job.
Understand the structure of a pyATS test script
A test script in pyATS, called an AEtest test script, specifies what the framework tests in the network. It is critical for network engineers to understand the structure -- or procedure -- of a test script because it provides detailed instructions for a successful pyATS job.
Wade outlined the structure of a pyATS test script, which includes three major components:
- Common setup. The common setup is where network engineers collect and configure the devices they plan to include in the testbed. During the configuration process, network engineers should configure the features on their devices that they want to include throughout the tests.
- Test cases. The test case section includes the specific, individual tests within the test script. This is where network engineers define the conditions for the scenarios they want to test. Examples of tests include configuration changes or functionality and security tests.
- Common cleanup. The common cleanup is where network engineers remove the previously added settings from the testbed to prevent remnants from the previous test interfering with subsequent tests.
When network administrators want to test parts of their network, they must guarantee the test case sufficiently outlines the scenarios and conditions to include in the test. Network administrators can easily repeat and reuse Python commands in pyATS, which simplifies the process and supports seamless repetition, Capobianco said.
Another best practice for testing -- regardless of whether network administrators use pyATS or conduct test-driven development -- is to test for one particular condition per test, Wade added. If network administrators test for multiple parameters in one test, it can create complexity and complicate the test process.
"If one factor were removed from the environment or doesn't need to be tested, you won't have to refactor your code," Wade said. "You can delete the test, and then it's clear and concise in the reporting aspect. You can easily know what passed and what failed."
This method also enables network administrators to easily repeat tests with different conditions to test other factors.
Integrate REST APIs
Network administrators can integrate pyATS with other frameworks and systems, including those beyond the Cisco library, for a more open experience. REST APIs, for example, can automate pyATS test cases. While pyATS has built-in REST APIs, network administrators can also use external APIs to connect pyATS to other services:
- PyATS APIs. Network administrators can use thousands of built-in, native APIs in the pyATS framework, such as .parse, .learn or Jinja templates.
- External APIs. Network administrators can also connect external APIs to pyATS. For example, network administrators can integrate other Cisco services with pyATS to retrieve data in JSON format, which is the format of a pyATS testbed. Any external API, whether Cisco or non-Cisco, can work with pyATS so long as it's in JSON format.
"The key is the JSON," Capobianco said. "It doesn't matter if it comes from a parsed show command or a parsed REST API -- the JSON is testable with pyATS."
Use additional Python libraries
Because pyATS is a Python-based framework, it uses Python libraries to automate and validate network changes and tests. It has essential libraries that it needs to use to function, such as Genie and Unicon, but network administrators can also apply additional Python libraries to pyATS to enhance its capabilities.
For example, Rich is a Python library that can add rich text, color and style to pyATS' command-line interface terminal. Network administrators can integrate pyATS with Rich to add colors to their outputs. Color-coded results can help sort the pyATS test script results into categories, Capobianco said. With Rich, network administrators can configure pyATS' log viewer to display passed tests in green and failed in red, which can quickly indicate how tests perform.
Rich is only one example of a Python library that can integrate with pyATS. Network administrators can use other Python libraries, such as the AI-based framework LangChain or the web development-based framework Django, with pyATS, Capobianco said.
"Because it's Python, you can really use your imagination and bring in other Pythonic libraries to enhance and augment your pyATS code," he said.
PyATS: Easy to support network admins of any level
PyATS requires network administrators to write Python code to run these test scripts and automate network tests, a skill many network engineers might not have mastered yet. But pyATS is a networking-specific framework meant to support professionals with their network testing and validation processes, as well as other additional use cases.
Even administrators with limited programming skills can deploy tests through pyATS without damaging the network. But following best practices can simplify the process for network administrators who want to ensure test scripts can run seamlessly.
"You [can] do that in minutes, with a very minimal amount of code. You don't need to be a Python expert or have 25 years of Python to embrace [pyATS]," Capobianco said.
In this excerpt from Chapter 4 of Cisco pyATS Network Test and Automation Solution, "AEtest Test Infrastructure," Capobianco and Wade provide a detailed look at how to use pyATS test scripts to run a pyATS job. Learn additional best practices to run successful pyATS jobs with detailed test scripts.
Learn more with Cisco pyATS Network Test and Automation Solution by John Capobianco and Dan Wade
Click here to read Chapter 4, "AEtest Test Infrastructure."
John Capobianco is a seasoned IT professional with more than 20 years of experience and a passion for automation. He has held various technical and leadership roles, including senior IT integrator and planner at the House of Commons of Canada and developer advocate at Cisco. Known for his innovative approach to network management, Capobianco is a recognized expert in DevOps and infrastructure as code. His work continues to shape the future of network automation.
Dan Wade is network and infrastructure automation practice lead at BlueAlly, specializing in developing and delivering advanced automation solutions. With more than seven years of experience in network automation, Wade is proficient in tools like Ansible and Terraform, as well as Python libraries, such as Nornir and Netmiko. Wade holds Cisco DevNet Professional and CCNP Enterprise certifications and is a member of the Cisco DevNet 500 and Cisco Champions programs.
Deanna Darah is site editor for TechTarget's Networking site. She began editing and writing at TechTarget after graduating from the University of Massachusetts Lowell in 2021.