Python is a general-purpose programming language with a relatively low learning curve. Its syntax is similar to other languages, making it a good starting point for beginners. But why choose Python for test automation?
Python offers two* very popular testing libraries: unittest and pytest, each with its own advantages and disadvantages. Python has gained popularity recently due to simplicity, versatility, and the extensive availability of libraries, tools and frameworks (Selenium, Playwright). This makes it an ideal choice for QA professionals with no prior programming experience. It's like the "low-hanging fruit" for those who need to dive into automation quickly.
However, the ultimate choice of the test automation language should align with the language used in the application you're testing. For example: If the developers haven't specified a preference or the application consists of multiple languages- Python can act as the "glue". Facilitating communication and integration between different components written in various languages that's easily understood by all parties involved.
Python's design emphasizes readability by following English language syntax. Its simplified form allows users to focus on the task at hand without being burdened by unnecessary boilerplate code. As a general-purpose language, Python can be used for server-side development, web applications, desktop applications, automation, glue code in AI models, and research. Different flavors of Python are available, such as IronPython (C compiler Python) and Jython/JPython with Java VM, allowing seamless integration with other languages.
To learn more about Python, you can visit the official Python website at https://www.python.org/.
For Python test frameworks, let's explore a trinity: unittest, nose, and pytest.
Unittest (built-in)
Python's built-in testing framework, part of the standard library.
Offers a comprehensive set of testing tools, including test discovery, test runners, and test case management.
Follows a class-based approach, where test cases are defined as sub-classes of unittest.TestCase.
Provides assertion methods and test fixtures for common testing scenarios.
Supports test suites and test runners for running tests in bulk.
Official documentation: unittest
Nose (deprecated)
Previously a popular testing framework, but now considered deprecated in favor of pytest.
Built on top of unittest and adds additional features and enhancements.
Supports test discovery, test case generation, plugins, and test coverage reporting.
Provides a more concise syntax compared to traditional unittest.
While still functional, it's recommended to use pytest instead for new projects.
Official documentation: nose
Pytest
A lightweight and easy-to-use testing framework with a focus on simplicity.
Supports test discovery, fixtures, parameterization, and powerful assertions.
Provides an extensive plugin ecosystem for additional features and integrations.
Emphasizes clean and readable test code with expressive syntax.
Well-documented and actively maintained by a vibrant community.
Official documentation: pytest
In summary, Python is an easy-to-learn, understand, and use programming language. It's versatile, free, and designed to be readable. In the upcoming posts, we'll be exploring Python test frameworks based on pytest and the plugins provided.
Now it's time to flex your Pythonic muscles!
Let's write our first Python script. We'll start with a simple program that calculates the average of a list of numbers. Remember to first install Python and add it to your PATH during the installation process. You can use IDLE, the integrated development environment that comes with Python, to write the code. IDLE will immediately show an error if you make any mistakes. Take note of how lines prefixed with # behave. Avoid copying and pasting code. Instead, type it out yourself. If you don't understand some parts at this stage, that's perfectly fine. You'll gradually learn about them as we progress.
Finally, think about whether this code could be written in a more compact or readable way. Remember that for every problem, there are multiple solutions available.
Python3:
# Take numbers as input from the user and store them in a list
list_of_numbers = list(map(int, input("Enter multiple values separated by spaces: ").split()))
# Calculate the sum of all the numbers in the list
sum_of_numbers = sum(list_of_numbers)
# Divide the sum by the total number of elements in the list to find the average
average = sum_of_numbers / len(list_of_numbers)
# Print the average to the console
print(average)
Let me know in the comments section how it went!
__________
*python has to offer also lettuce, behave, nose2, testify, robot and others.
Nice intro, thanks a lot!