Python Login Register JSON Example

This example will show you how to read / write JSON text from / to json file. It will also show you how to create TestCase class to run unit test for different class function. And it also tell you how to generate html reports for the unit test result.

1. Python Login Register JSON Example Overview.

This example will create a python class UserAccount, it provide two function login and regist.

The login function will accept user input username and password arguments and verify them in a JSON format text file user_account.json.

The regist function will save user input username, password and email in the user_account.json file. But first this function will check whether the username has been used or not.

If login / register success, it will return true with success message, if login / register fail it will return false with error message.

So in this example, user account info are saved in JSON file, so we need to use Python json module. If you want to learn more about JSON operation in Python, you can read Python JSON Dump To Or Load From File Example

This example also use Python unittest ( Used to run unit test ) and html-testRunner ( Used to generate unit test html report ) module to run unit test for login and regist function. You can read Python 3 Unittest Html And Xml Report Example for detail.

2. Example Source Code.

Below is the example file structure in eclipse PyDev package explorer. This example only use the file.

python login regist json unit test example file

Below is the unit test html report web page.

python login regist unit test html report

Created on Aug 19, 2018
@author: zhaosong

import unittest, json, os, HtmlTestRunner

# Define UserAccount class.
class UserAccount(object):

    # Returned dictionary status key
    ret_status = 'status'

    # Returned dictionary message key.
    ret_msg = 'msg'

    # This file is used to save user account in JSON format.
    # The data format is something like '{"Jerry": "jerry,"}':
    account_save_file_path = './user_account.json'

    # Login with user_name and password.     
    def login(self, user_name, password):
        ret = {}
            # Load exist user account from user account json file.
            file_obj = open(self.account_save_file_path, 'r')
            exist_user_account = json.load(file_obj)

            # If the user_name related string do not exist.
                ret[self.ret_status] = False
                ret[self.ret_msg] = "User name do not exist."
                # Loop for all the dictionary items.
                # You can use exist_user_account[user_name] directly to check password,
                # below code just for demo dictionary items iteration.
                for key, value in exist_user_account.items():
                        # Split the password,email
                        list = value.split(",")

                        # Get password value.
                        tmp_password = list[]

                        if(tmp_password == password):
                            ret[self.ret_status] = True
                            ret[self.ret_msg] = "Login success."
                            ret[self.ret_status] = False
                            ret[self.ret_msg] = "Password is not correct."
        except FileNotFoundError:
            ret[self.ret_status] = False
            ret[self.ret_msg] = FileNotFoundError.__cause__
            return ret

    # This function is used to regist user_name, password and email in ./user_account.json file.
    def regist(self, user_name, password, email):
        ret = {}
        exist_user_account = {}

        # If the user account file exist.
            # Load the JSON content in the file.
            file_obj = open(self.account_save_file_path, 'r')
            exist_user_account = json.load(file_obj)

            # Loop in all user name if exist then tell user can not regist this one.
            for key in exist_user_account:
                if(key == user_name):
                    ret[self.ret_status] = False
                    ret[self.ret_msg] = user_name + " exist, please use another one."
        # If the register user_name do not exist.     
        if(ret[self.ret_status] != False):
            # Open user account file with write permission.
            file_obj = open(self.account_save_file_path, 'w')
            # Construct user account data the format is username:password,email
            # And add the new user account data to the exist_user_account dictionary.
            exist_user_account[user_name]=password + "," + email

            # Save new user account data in the JSON file.
            json.dump(exist_user_account, file_obj)
            ret[self.ret_status] = True
            ret[self.ret_msg] = 'User account register successfully.'
        return ret
# TestUserAccount extends unittest.TestCase class.
# This test case class is used to test UserAccount login and regist function.
class TestUserAccount(unittest.TestCase):

    This class is used to make unittest for UserAccount class.

    def test_regist(self):
        user_account = UserAccount()
        ret = user_account.regist("Jerry", "jerry", "")
        # Assert regist method return data.
        self.assertTrue(ret[user_account.ret_status], ret[user_account.ret_msg])
    def test_login(self):
        user_account = UserAccount()
        ret = user_account.login("Jerry", "jerry")
        # Assert regist method return data.
        self.assertTrue(ret[user_account.ret_status], ret[user_account.ret_msg])
# This is common function which can run special test.       
def run_test_suite(test):
    test_suite = unittest.TestSuite()

    # Create HtmlTestRunner object and run the test suite.
    test_runner = HtmlTestRunner.HTMLTestRunner(output="./")   
def run_test_login():
def run_test_regist():
    # Run test login
    # run_test_login()
    # Run test regist
    # run_test_regist()
    # Run all test function.
(Visited 329 times, 1 visits today)
READ :   How To Convert JSON File To CSV Using Python Vice Versa

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.