Test API Python automation with
a friendly Tkinter APP

In this step-by-step tutorial, we delve into the dynamic world of Python programming. We will explore the rationale behind utilizing Tkinter for API testing, employing the Newman command for streamlined reporting. It covers the starting installation and delves into creating a complete Python Tkinter testing application, from GUI styles to executing API tests seamlessly to visualize results in real-time.

My name is Pedro, I’m from Brazil and I’ve been working as a QA Engineer for 5 years. My background is in automation using Selenium with Python, Selenium with JAVA, Cypress, CI/CD pipelines (with Jenkins, Azure Pipelines and Git Actions) and also API tests using Newman and Python. Currently, I work for Integritas Inc, a company from the United States as a QA.

I’m very keen on programming, especially in Python and now I would like to show you its tool like Tkinter widely used for building Python Apps. I hope you like my tutorial very much.Let’s take a closer look at this combination below!

Of course, you might think 🤔 Why not utilise all-in-one solutions? I chose ✅ it for API testing using the Newman command instead of HTML reports because it can be very useful for short APIs or only to make sure that some microservices are working properly without requiring long reports and generating many files. Usually, I use it for microservice testing on my project App.

Before starting this guide, it would be a good idea if you had a look at some articles related to API tests with Postman and Newman, plus making your Python APPs with Tkinter:

Namely, they are:

And please make sure to pay attention to the article on this blog. Opting testomat.io testing tool also could serve as an alternative to get informative Python test reporting swiftly.

Pre Requirements:

  1. Install python3 and pip3 in your system
  2. Then, install the following Tkinter packages:
    sudo apt-get install python3-tk
  3. This project was created in an OS Ubuntu 20.04
  4. Install Newman in your system.
  5. Create a folder for saving the files of your Tkinter App.
  6. Export your Postman collection and Environment to the repository of your Tkinter App.
My API project looks like this screenshot

👉 Let’s start the creation of our Tkinter testing App. The full code you can find in my GitHub repository.

First of all, create a Python file called menu.py Next, import the tkinter lib and create a class called Application which will contain all the elements of the main menu. Then, call the screen by adding the if __name__ == "__main__": instructions:

Python class instruction and object on the program code

As a result, if you run your Python file, you should see the Thinker GUI window.

Python Thinker GUI

Create a new method within the class Application called screen with some information of the main menu and call this method when initializing the class:

Initializing Python class in our testing App

Create a new method within the class Application called screen_buttons including the information of the Python Tkinter style buttons in the main menu and call the method in the class initialization:

Python class to add style to button on Tkinter

Here are the button styles; You will see how they work in the following section of our Python Tkinter testing tutorial. The full code of our testing App is here

def screen_buttons(self): 
    self.bt_execute_api_tests = Button(self.main_screen,
 
            text="Execute API Tests", 
            border=2, 
            bg="#9ac7d2", 
            font=('verdana',10, 'bold'), 
            activebackground='#108ecb',
            activeforeground='white',
    command=self.openExecuteAPI
        ) 
    
    self.bt_execute_api_tests.place(
            relx=0.11, 
            rely=0.22,
            relwidth=0.2, 
            relheight-0.06
        ) 
    
    self.bt_quit = Button(self.main_screen,
 
            text="Quit Menu", 
            border=2, bg="#9ac7d2", 
            font=('verdana',10,'bold'), 
            activebackground='#108ecb', 
            activeforeground='white',
 
    command=self.main_screen.destroy
        ) 
    
    self.bt_quit.place(
            relx=0.11, 
            rely=0.52, 
            relwidth=0.2, 
            relheight=0.0
        )

Right after creating the method screen_buttons, create a new class called ScreenFunctions(), in which you put all the methods to redirect to the API tests page when we click on the button self.execute_api_tests.

Python implementation of redirect method

Now, create a new file under the APP folder called execute_api.py. Under this file (execute_api.py), import the lib Tk and then create a class also called as Application in which you should write the following code:

Call function

Come back to the file menu.py, and import the execute_api file by writing the following line:

Importing module of our App

Include the class ScreenFunctions within the Application class as Application(ScreenFunctions) in the file menu.py

Tkinter testing App launching

Execute the main menu again by opening the menu.py file. You should get the following screen:

Midway result of Tkinter testing App

If you click on the Execute API Tests, you should be brought to the screen initialized in the execute_api.py file. In the case you click on Quit Menu, the app will be closed.

Now, please open the file execute_api.py and let’s create the screen for executing our Newman tests through the methods attached to the buttons of the page by importing the Tkinter library in the execute_api.py file.

Our following step is to create some global variables identifying our folders and the Newman commands to execute each folder separately. In our example, our Postman collection has the following structure:

Structure of API project tree

Considering that, create the following variables.

🔴 Note: The Execution of all folder indicates that you want to execute the collection as a whole, not only one specific folder.

Variable commands_to_execute_tests indicates each Newman command to execute the tests for each collection folder.

Execution of Newman collection

As in the menu.py file, create the ScreenFunctions class and create a function for returning to the main menu. Include class ScreenFunction in the Application class (same as did for the menu.py file).

Create a method called screen under the class Application and call it in the initialization to set up the screen.

🔴 Obs: Import the menu file to make sure it will be called!

Implementation of the application menu

Also, create two variables called x and y that will be used to place some buttons with the name of each folder and execute all options as well.

Our variables

Under the ScreenFunctions class, create a function called create_button as the following code:

Method for the button of our testing App

Now, within the Application class, create a method called buttons which will be in charge of the creation of the buttons displayed on the screen.

Our Python method

Call this function in the initialization of the class Application

Initialization of our testing App

If you open your Python file execute_api.py, you should have the following result:

One more midway result of our Reporting Testing App on Tkinter CUI

Under the class Application, create a method called text_area which will send the terminal output generated from the Newman command execution. Call this method in the initialization:

Sending output

The last method within the class Application is called  scroll_bar_screen, it will be used to scroll up and down the text area for looking at the output information from Newman execution (also call it in the initialization of the class Application):

Scrolling

Import the following libraries in the file execute_api.py before starting the final steps. Also, create two more global variables called  stop_thread and call it.

Let’s do the final steps to show the results of the Newman command in our screen in the file execute_api.py. Under the class ScreenFunctions, create the following methods as shown below clock method.

For full code follow through the link

Now, enjoy your own Python Tkinter testing App 🙂 and execute your Newman tests in a friendly and easy way by clicking on the folder or executing everything, as you prefer. Hope you revolutionize your approach to API testing!

Real-time reporting Python Tkinter & Postman Newman API test collection

👉 Please let me know if you have any questions, reach out to me at my LinkedIn profile Pedro Carvalho. Once again I provide a Github repo of this project with full code. It will help you understand how the code works and how you can customize my experience for your test projects.

Analytics Dashboard 📊
Unlock potential of Real-time reporting and elevate API testing your project.
Follow us