Thursday, August 10, 2023

Why should Test Automation Engineers learn Python?

 Introduction:

This question could be bothering a lot of test automation strategists. Java, C#, Groovy and JavaScript’s are the major programming or scripting languages dominating the test automation space for long. I also explained how a programming languages are chosen for test automation in my previous blog. Checkout this link, here.

So, what’s special about Python? In short, Python is an interpreted language, supports majority of operating systems with .NET framework installed, supporting high level programming, including object-oriented programming and offers numerous choices of powerful libraries for AI and ML, which could be integrated into your automation framework to make it AI/ML enabled. Both PyTest and RobotFramework are very popular, easy to learn, powerful test automation frameworks built on Python. RobotFramework supports a wide range of automation capabilities, like UI, E2E, RPA, Salesforce, Workday, SAP, API and Mobile. PyTest backed frameworks are extensible using Python libraries.

The dilemma on Test Automation Engineers minds:

First is assertiveness about there won’t be any decline on Java/C#/JavaScript automation demands. For example: “I have been working with Java backed test automation from past 10 years, it won’t be easy to acquire Python skills and become proficient in it within few weeks” – such proposition is not always true. Because, you might have worked with only core-java with Selenium / Appium without Spring or Hibernate frameworks, you might have only used TestNg and not Junit, you might used Junit 4 but not Junit 5. You might not try Lombok annotations, running batches with Jsch and reading console logs, connecting to JDBC and executing SQL queries, using JMS for message queues, integration with Swager/pact, JSON/XML parsing. These all are related to Java and required for E2E automation, if you did not work in all these areas in your 10 years journey, and you are not confident on your proficiency in these areas, then you cannot justify your love and commitment with Java.

In today's world, the test automation demands are volatile, recently the curve is going up for C# and JavaScript backed test automation, and it’s going down for java. Python backed automation demand is at equilibrium.

Second, “the syntax is completely different and not easy to remember, not as friendly as java” – such proposition is also not always true. The basic of all programming languages remain same, if you create own mental barriers, then you will never be able to focus on the learning. The Generative AI, can write better code than most of us, if it can learn, then why can’t we?

Our future automation will include more of AI/ML:

As more and more organizations undertaking their AI/ML enablement journeys, our future automation work will involve a lot more testing around AI/ML training data models, predictable complex api’s, big data analytics and numerical analysis. Python is in forefront, and a language of choice for AI/ML programming. So, the more delay we do to appreciate these facts, we will be fallen behind and will repent missing out on the growth opportunities in our career.

Is Python easy to learn:

I would say yes, otherwise in India it should not feature in School level curriculum. From my personal perspective also, yes, it is easy to learn with an open mind, overcoming our preferred programming language biases and psychological resistances, focusing on important semantics which helps us learn by analogy.

Below example explains a simple python program to help you learn by analogy:

#Copy the code in a file and save with .py extension

#Ensure Python 3.11 is installed in your system

#To execute the file, use any command line tool, type: python <filename>.py

#At first, we are creating a class file, with two methods, "init" represent a parameterized constructor method, and "str" represent a special method which instructs how to print the class objects in output, similar to overriding "toString()" method in java. As you can see, there is no need to specify access modifiers, return type, semicolon, curly braces. Python follows strict indention, as long as you are adhering to it, everything works fine. The "brand" and "engineCapacity" are the instance variables.

class Car:

    def __init__(self, brand, engineCapacity):

        self.brand = brand

        self.engineCapacity = engineCapacity

    def __str__(self):

        return f"Brand = {self.brand} and Engine_Capacity = {self.engineCapacity}"

#The "main" method is equivalent to Java/C# main method, but lot simpler in its declaration. The below code is responsible for running the Python program

if __name__ == "__main__":

    #Creating two Car objects

    firstCar = Car("Maruti","1000CC")

    secondCar = Car("Lamborgini","2500CC")

    #Creating a list with the two Car objects created above

    cars= [firstCar, secondCar]

    #Loop construct is similar to "for each" in java,

    for x in cars:

        print(x)

    #The len(list/string) method returns the length of a list or string

    print("Total items in cars list: ", len(cars))

    #Python lists support pop method, which can remove an item from a list with the specified index

    cars.pop(1)

    print("Removed 2nd item from cars array using list pop method, so Lamborgini will no longer be listed")

    for x in cars:

        print(x)

        print("index of "+ str(x) +" is "+ str(cars.index(x)))

    print("Using list append method to add one car to the cars list, the item will be added at last")

    #We can append an item to the tail of a list using the append method

    cars.append(Car("Hyundai","1299CC"))

    for x in cars:

        print(x)

    print("Using list insert method to add one more car to the cars list, at index 1")

    #We can append an item to a given index in a list using the append insert method

    cars.insert(1,Car("Kia","1200CC"))

    print("Present items in cars list")

    for x in cars:

        print(x)

    #Below code iterates a string in a reverse method, there are more simple techniques available

    #to achieve the same

    ms = "Hello"

    for x in range(len(ms)-1, -1, -1):

        print(x)

        print(ms[x])

    #Below code removes duplicate numbers from the list

    my_list = [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]
    for i in range(0,len(my_list)):
        for j in range(i+1,len(my_list)):
            if (j<len(my_list) and (i == my_list[j] or my_list[j] in my_list[0:j])):
                del my_list[j]
    print("The list with unique elements only:")
    print(my_list)

Summary:

In this blog, I just tried to establish a proposition, discussed various aspects around it and cited some  examples. Hope you will find the post useful and share with your friends. Do write comments to share your opinion, it matters most.

No comments:

Post a Comment

Test Automation Strategy for Oracle Forms application running in Citrix servers

  Context : There are many product based applications developed using Oracle Forms and Java thick-client architecture, and most of them are ...