Python fundamentals for automation test
Tags: python, pytest, automationtest
Published: 2019-09-20 14:26:56

---
title: Python fundamentals for automation test
published: true
description: 
tags: #Python #pytest #automationtest
---
    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

I. About Python

Python is a general flexible programming language.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/y278i0nqc1tfw7nkiso0.jpg)


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:

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


![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/j2cjzkoazq7x0tf8hgmz.PNG)


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](https://thepracticaldev.s3.amazonaws.com/i/xxpflresrilchtp782ov.png)

2. Check for installation correctly or not:

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

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/ob0exzimimn4ke7fd962.PNG)



Check if python is installed correctly:

Open Command Prompt -> python --version

→ It should shows the current python version is : 3.7.4

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/fmq5my7ss79quwcxjfm5.PNG)

    Some simple REPL python:

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


```javascript
→ print('Hello World')

a = 5

b = 100

a+b → 105

quit() to end current repl

```
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/h16x4hiulzs0ismwwgbl.PNG)

3. 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

4. For Linux:

Please follow this guide python-for-linux
5. 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:

Go to pip official site for installation :  Pip official site

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

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/ms3ctwmsoh27ffp331ll.PNG)

2. Install other libs

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

pip install <package_name>

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](https://thepracticaldev.s3.amazonaws.com/i/3l8j9d2z8kn0clebjb5z.PNG)

    Or you can install the local lib from source code by:

pip install <path_to_package_source_code>

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

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/r6lh6osnztht5dbckf2k.png)

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/i3ub6ivnrwga3ihls9ku.png)


2. 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
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/bmtm8ydulnobi73pa73p.PNG)


  →

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/ennftxk6j6bm2v283vo5.PNG)


    Clone project in pycharm

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 :

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

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

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

For example : string type is immutable

But list is mutable

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/4zcy14gcn72doray6u1y.PNG)


3. 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):