These days, testing is a necessary part of the development process but not all software companies have a testing department. So, software engineers need to have checking skills to complete projects without a professional tester. So, I have been persuaded that testing is a critical experience while processing software.

In theory, There are several well-known types of tests:

  • Unit test

  • Integration test

  • Functional test

  • End-to-end test

First and foremost, unit test is a specific inspection process helping developers review code exactly. To make it more clear, the code with the function is investigate program (In Other Words, program check program). It helps people check applications still run without having an intense effect. Let's see on Flask Tesing library. I use it in example below.

              
import json
import pytest
from apiapp.main_api import app # Import your application instance
 
@pytest.fixture
def client():
    with app.test_client() as client:
        return client
 
 
def test_register_validate_input(client):
    response = client.post(
        "/api/report/balance", json={
            "start_date": "2021-11-01 02:00:00"
        }, headers={'Authorization':'YourToken',
                     'Content-Type': 'application/json'
                     }
    )
    if response.data:
        data = json.loads(response.get_data())
        assert 'message' in data
              
            

However, unit test can not check function which clients usually typing in UI/UX.


Second, selenium is a human reaction simulator on screen as Functional test or End-to-end test. This opinion is held because selenium handles actions as humans do it such as opening popup, submitting a form. When a team accomplishes a new version production, selenium helps testers not spend time checking old functions again. Team just runs a selenium function which has defined actions on screen. In practice, I usually go through on SeleniumBase and Faker


              
from seleniumbase import BaseCase
from flaky import flaky
import os
from faker import Faker
 
IP = os.getenv("IP", "localhost")
PROTOCOL = 'https'
f = Faker()
 
 
class EntityPageTest(BaseCase):
    '''
    Documents:
    https://pythonrepo.com/repo/seleniumbase-SeleniumBase-python-testing-codebases-and-generating-test-data
    '''
    input_mapping = {'input#reg_num_entity': f.numerify(),
                     'input#place_of_incorporation_entity': 'American\n',
                     'input#entity_type_entity': 'Company\n',
                     '#entity_name_entity': f.name(),
                     '#display_name_entity': f.name(),
                     'input#sponsor_entity': 'E000001\n',
                     '#tin_entity': f.numerify(),
                     '#giin_entity': f.numerify(),
                     'input#industry_entity': 'Accommodation and food service activities\n',
                     'input#func_cur_entity': 'AFN\n',
                     '#comments_entity': f.text()
                     }
 
    @flaky(max_runs=1, min_passes=1)
    def test_contact_creation(self):
        # If use Basic authentication
        self.open(f"{PROTOCOL}://{IP}/web/app/")
        self.click("#tcgSidebarBtn")
        self.click('a[href*="/app/entity"]')
        self.click('#collapse_btn_entity')
 
        for key, value in self.input_mapping.items():
            self.type(key, value)
        self.click('#form_submit_btn_entity')
        flash_class = self.wait_for_element_present(
            "#flash_entity", timeout=30).get_attribute('class')
        assert 'success' in flash_class

              
            


In conclusion, Testing is an indispensable process in software development. Selenium tools bring more benefits to the team than a traditional method which has lots of human resources.

Steve Nguyen

© Steve CV. All rights reserved.
Design - TemplateFlip