DEV Community

cuongld2
cuongld2

Posted on

Python fundamentals for automation test

I. About Python
II. Install Python :
    Get and install from official site:
    2. Check for installation correctly or not:
    3. For MacOS:
    4. For Linux:
    5. Python virtual environment:
III. Install pips and other libs:
    Install pip:
    2. Install other libs
IV. Git Installation
V. Pycharm IDE
    2. Simple Configuration and usage:
        2.1 : Fresh new project:
        2.2 : Clone projects from other source:
VI. Python types and basic structures
Enter fullscreen mode Exit fullscreen mode

I. About Python

Python is a general flexible programming language.

Alt Text

Currently we have 2 major versions of Python : Python 2 and Python 3. But Python 2 will not be supported from 2020.

Besides, Python3 with a lot of active changes make it easy for development and maintenance ( easy to handle with strings, non ASCII character..)
II. Install Python :

Get and install from official site:
Enter fullscreen mode Exit fullscreen mode

Please go to official site of Python for download : Python download site

Alt Text

Go for the latest version : currently latest version is 3.7.4

Notes: In Win 7, please if install Python 3, please install version 3-7-3 ( does not work with 3-7-4)...

Install Python from exe

Alt Text

  1. Check for installation correctly or not:

After installation success, go to system environment variables to check for path.

Alt Text

Check if python is installed correctly:

Open Command Prompt -> python --version

→ It should shows the current python version is : 3.7.4

Alt Text

Some simple REPL python:
Enter fullscreen mode Exit fullscreen mode

REPL stands for Read Evaluate Print Loop, and is the name given to the interactive MicroPython prompt that is accessible on the Pycom devices

From Command Prompt :→ python
Enter fullscreen mode Exit fullscreen mode
 print('Hello World')

a = 5

b = 100

a+b  105

quit() to end current repl

Enter fullscreen mode Exit fullscreen mode

Alt Text

  1. For MacOS:

Usually python2 is already installed in MacOS and you cannot uninstall python2

Next thing you need to do is to install Python3 along with existing python2

There are 2 options to install Python3:

Go to Python official site and download the package to install
Install python via homebrew
Enter fullscreen mode Exit fullscreen mode
  1. For Linux:

Please follow this guide python-for-linux

  1. Python virtual environment:

    Manually create virtual environment : virtual environment python
    Or you can create project with virtual environment interpreter by your favorite IDE (For example : Pycharm)

III. Install pips and other libs:

Install pip:
Enter fullscreen mode Exit fullscreen mode

Go to pip official site for installation : Pip official site

Check if pip is installed by this command: pip --version

Alt Text

  1. Install other libs

    Most of the times, you would install python lib via pip

pip install

For current usage, to install all required libs that take to do automated testing in python, please install with the following requirements.txt

text fileEdit file

pip install -r requirements.txt

To verify if the library is installed or not, type in command prompt, for example to check pytest is installed or not: pytest --version

Alt Text

Or you can install the local lib from source code by:
Enter fullscreen mode Exit fullscreen mode

pip install

For example:

pip install C:\Users\cuongld\PycharmProjects\pytest-testrail
IV. Git Installation

Go to git installation page : git-installation-page

Check git is installed or not : git --version
V. Pycharm IDE

There are 3 major versions of Pycharm : Enterprise, Community, Education

Download and and install

Download and install pycharm community from jetbrains site : Pycharm IDE
Enter fullscreen mode Exit fullscreen mode

Alt Text

Alt Text

  1. Simple Configuration and usage: 2.1 : Fresh new project:

File → New project → Choose the framework to work with or pure python
2.2 : Clone projects from other source:

I will give example for github project

Go to github page stored project
Enter fullscreen mode Exit fullscreen mode

Alt Text

Alt Text

Clone project in pycharm
Enter fullscreen mode Exit fullscreen mode

VCS → Check out from version control → Git → HTTPS Clone

(If the project is private, or only allowed certain accounts to access→ you may need to input username, password in pycharm)

Or by command line :
Enter fullscreen mode Exit fullscreen mode

Go to the location for stored project source code

cd stored_project

git clone https://username:password@github.com/username/repository.git
After clone successfully, open project in Pycharm:
File -> Open -> Choose project directory.

VI. Python types and basic structures

Every things is an object
Enter fullscreen mode Exit fullscreen mode

For example, in Java, to create a function, you must assign that to be long a class ( or interface..)

But in Python, you can create a function alone in the module

  1. Types can be "immutable", "mutable"

For example : string type is immutable

But list is mutable

Alt Text

  1. Basic usage:

    Create new instance of class: instance_class = Class_name()
    Python use indentation for understand code block
    To call a function in within a class, must use with self, for example :

class SettingsElements(BasePageElement):

def find_continue_where_left_off(self,driver):

 self.find_shallow_element()

Import can be local or global in module
Inheritance from parent class : class name (BaseClassName):
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
famduong profile image
PhamDuong

hey guy. this post is big help for my first steps on automation testing. Thanks a lot! :D

Collapse
 
cuongld2 profile image
cuongld2

Thanks for your comment.
It means a lot to me.
Other incoming posts awaited.
Stay tune.