用Python自动化软件测试

用Python自动化软件测试

发现产品中的故障的过程被称为软件测试。此外,它评估实际结果是否与预期结果相符,并帮助发现错误、缺失的需求或空白。在产品进入市场之前的最后一步是测试。它涉及查看、研究、观察和评判各种产品组件。行业中的软件测试人员结合人工和自动化测试方法。测试人员在运行测试后将结果通知开发团队。软件测试是必不可少的,因为最终目标是为用户提供高质量的产品。

许多企业都忽视了对产品的测试。他们可能声称财务限制是跳过这一关键阶段的原因。他们认为这对产品影响不大。然而,为了在市场上建立一个强大和良好的初始印象,产品必须卓越。这就需要对产品进行彻底的错误测试。

提高产品质量

只有在产品完美无缺时,公司才能向其客户提供价值。为了实现这一点,企业必须确保用户在使用其产品时没有任何问题。使您的产品不含错误是实现这一点的可靠方法。

在交付产品之前,企业必须专注于测试应用程序并修复在测试期间发现的任何缺陷。当团队在产品到达客户之前修复问题时,产品的质量就会提高。

提高安全性

当客户使用产品时,他们很可能会透露某种类型的个人信息。为了防止黑客访问这些信息,需要进行安全测试,确保程序部署之前。当公司遵循全面的测试程序时,它保证了一个安全的产品,帮助客户在使用时感到舒适。 银行应用和电子商务商店例如需要支付信息。如果开发人员不修复与安全相关的缺陷,可能会导致巨大的财务损失。 安全的另一个方面是防止数据丢失。如今人们经常将数据保存在云存储上。你的照片和文件可能也存储在iCloud或Google Drive上。 如果出了什么问题,你的所有数据都丢失了怎么办?这不是你最大的恶梦之一吗?产品的安全性确保数据不仅受到黑客的保护,而且不会丢失或损坏。

客户不再只是使用大型桌面计算机。在当今以移动设备为主的世界中,测试产品的设备兼容性至关重要。想象一下你的公司创建了一个网站。测试人员必须看到该网页在各种设备分辨率下是否起作用。它还应与各种浏览器兼容。浏览器选择的增加是测试变得越来越重要的另一个原因。在Chrome上有效的内容可能与Safari或Internet Explorer不兼容。这就需要跨浏览器测试,即验证应用程序在多个浏览器上的互操作性。

软件测试的分类

软件测试是一个广泛的术语,涵盖了各种活动。然而,它并不以一个形式存在,可以根据多种因素进行分类。例如,您可以将测试分为手动测试和自动化测试。当涉及到自动化测试时,您可以选择基于代码和无代码的方式,或者结合两者的优点的混合方法。还可以根据测试对测试对象的内部实现了解程度的多少来分类测试。根据这个标准,我们可以将测试分为白盒测试、黑盒测试和灰盒测试。最后,我们可以根据测试是否确认应用程序的业务需求将其分为功能性和非功能性。

黑盒测试:

您对产品的开发方式了解最少是在这种测试中。您对产品的结构、编码或逻辑没有任何了解。您将以客户的身份使用产品。黑盒测试用于功能测试,因为您和客户拥有相同的知识量。只有在运行代码后,才能进行这种形式的测试。因此,采用动态测试。动态测试是一种测试方式,您必须在代码运行时运行代码并测试产品。主要是为了了解产品上线后的工作情况以及用户的反应。

灰盒测试:

在这种测试中,您只对产品有有限的了解。这种测试形式适用于发现用户不知道的问题。

举个示例,假设您创建了一个带有绿色阴影的元素,但实际上是蓝色的。用户不会意识到存在缺陷,因为他们认为这就是应该的。但是,您对产品的有限了解将有助于您发现此类缺陷。

Monkey测试

Monkey测试是一种软件测试方法,通过使用各种随机输入重复测试一个模块,以确保其无错误。只有少数选定的系统模块用于测试,目的是评估该模块是否正常运行。这种形式的测试是手动进行的,而且频率很高。描述Monkey测试的术语包括“折磨测试”、“容错测试”和“不愉快测试”。

现在您了解了测试的所有内容,现在是时候学习如何进行软件测试了。

  • 计划: 操作始终始于计划。在此阶段,您编制所有相关产品细节的清单。您首先创建需要测试的任务列表。如果您在修复错误后进行测试,则应了解问题是什么以及理想行为是什么。另一个问题是优先处理待办事项列表。在整个团队参与的情况下,也可以在此阶段进行任务分配。
  • 准备: 在确定需要完成的任务后,您需要为测试做好准备。准备测试环境,收集测试用例,并研究产品功能和测试用例均属于此过程。同时,收集和熟悉测试工具和方法也应在此阶段完成。
  • 执行: 这是您真正对产品进行测试的阶段。您运行测试场景并记录结果。然后将结果与预期结果进行比较,以评估产品是否按预期运行。您会跟踪所有成功和失败的测试和测试用例。
  • 报告: 这是软件测试的最后阶段,您必须记录所有结果并将其发送给相关人员。在这种情况下,测试用例的失败尤为重要。应该对已进行的测试和获得的结果有适当和清晰的描述。在更复杂的测试中,还应包括复制错误的步骤、截图和其他相关信息。

软件测试的好处

  • 高性价比: 这是软件测试的最重要好处之一。准时进行软件测试的任何IT项目都能帮助您在长期来节省资金。在软件测试过程中及早发现并纠正问题比较便宜。
  • 安全性: 这是软件测试最脆弱且敏感的优势。人们寻求可信赖的产品。它有助于提早发现风险和问题。这也是应避免软件测试的另一个重要原因。据说这是身体最脆弱和敏感的部分。有很多情况用户的信息和详细资料被获取和用于个人利益。据说这是为什么客户寻求经过彻底测试并被证明可靠的产品的原因。随着产品经过测试,用户可以确信他们将获得一个可靠的产品。用户的个人信息得到了保护。借助软件测试,用户可以获得没有漏洞的产品。
  • 产品质量: 这是任何软件产品的先决条件。测试确保买家获得高质量的产品。
  • 客户满意度: 任何产品的主要目标都是提供客户满意度。通过UI/UX测试确保了最佳用户体验。产品所有者的根本目标是提供最高水平的客户满意度。软件测试的重要性可以归因于它提供所需要的和理想的用户体验。如果在竞争激烈的市场中选择了理想的项目,您将能够建立可靠客户的声誉。因此,选择软件测试将为您带来长期的好处。赢得客户的信任并不容易,特别是如果产品始终被证明存在故障。您可能已经尝试了很多产品,并且有很多不好的经验,因此可能已经移除了该应用。在今天的市场上,市场竞争非常激烈。初次印象非常重要,如果您未能留下好印象,人们将寻找满足他们所有需求的其他产品。

现在让我们来看一下使用Python编程语言自动化软件测试的一个示例代码:

代码

#A sample python code to automate the testing of the sample class having basic functionalities of to perform the mathematical operations on the specified operands of that particular function.

# All the required modules which are required across the program are included at the beginning of the code.
import unittest
import sys

A class is written which has different functions to perform the different mathematical operations depending upon the input provided the mathematical operation is performed on the two operands which are taken as input for each function,  the class which is written below is the child class of the test case class from the unit test module,  with the help of this parenting test case class we were able to use various inbuilt functions of the test case class which help us to write various test case function to test the mathematical results which are calculated by the various function written inside this class 
class BasicMathOperationsTesting(the unit test.TestCase):
#Constructor is written which can be used to initialize the various class variable that is required to use throughout this class a variable that is declared inside the constructor of a class can be used across the scope of that particular class 
    def __init__(self):
        pass


# A function is written  in which the user is asked for two numbers on which the addition operation is performed,  once the user provides the two numbers on which the addition operation is need to perform,  that particular operation is performed on the provided two numbers and the result of that operation is stored in a resultant variable  and that resultant variable is returned as the return value of this function,  and then this return value can be used in various test function which will validate or test the particular operation performed by this function is correct or not by using various inbuilt methods of the test case class 
    def perform_addition(self):
        operation_name = "addition"
        print("Enter the first number for {} operation".format(operation_name))
        num_1 = int(input())
        print("Enter the second number for {} operation".format(operation_name))
        num_2 = int(input())

        resultant = num_1 + num_2
        return resultant

# A function is written  in which the user is asked for two numbers on which the subtraction operation is performed,  once the user provides the two numbers on which the subtraction  operation is need to perform,  that particular operation is performed on the provided two numbers and the result of that operation is stored in a resultant variable  and that resultant variable is returned as the return value of this function,  and then this return value can be used in various test function which will validate or test the particular operation performed by this function is correct or not by using various inbuilt methods of the test case class 
    def perform_subtraction(self):
        operation_name = "subtraction"
        print("Enter the first number for {} operation".format(operation_name))
        num_1 = int(input())
        print("Enter the second number for {} operation".format(operation_name))
        num_2 = int(input())

        resultant = num_1 - num_2
        return resultant
# A function is written  in which the user is asked for two numbers on which the multiplication operation is performed,  once the user provides the two numbers on which the multiplication  operation is needed to perform,  that particular operation is performed on the provided two numbers and the result of that operation is stored in a resultant variable  and that resultant variable is returned as the return value of this function,  and then this return value can be used in various test function which will validate or test the particular operation performed by this function is correct or not by using various inbuilt methods of the test case class

    def perform_multiplication(self):
        operation_name = "multiplication"
        print("Enter the first number for {} operation".format(operation_name))
        num_1 = int(input())
        print("Enter the second number for {} operation".format(operation_name))
        num_2 = int(input())

        resultant = num_1 + num_2
        return resultant

# A function is written  in which the user is asked for two numbers on which the division operation is performed,  once the user provides the two numbers on which the division  operation is needed to perform,  that particular operation is performed on the provided two numbers and the result of that operation is stored in a resultant variable  and that resultant variable is returned as the return value of this function,  and then this return value can be used in various test function which will validate or test the particular operation performed by this function is correct or not by using various inbuilt methods of the test case class
    def perform_division(self):
        operation_name = "division"
        print("Enter the first number for {} operation".format(operation_name))
        num_1 = int(input())
        print("Enter the second number for {} operation".format(operation_name))
        num_2 = int(input())

        resultant = num_1 / num_2
        return resultant

# This is a test function which is written for testing the assert equal values which means  this test case will be fast if the two values which are passed as parameters to this function  are equal,  so we have used this assert equal functions to test the various mathematical operations which are performed using the various function which is written above so, for example, let's say perform the addition operation of two variables which are passed as a parameter to that function so to verify that the addition operation performed that particular function is correct or not is then with the help of this function in which first parameter we passed as the actual value will be the value which  is the value which is calculated by the function which is written above and the expected value is the value which is expected to be matched with this function so so if both of these values matches then they assert equal test case will be passed on the other hand if the both of these values are not equal then the assert equal test case will fail 
    def the unit test_for_assert_equals(self,actual_value,expected_value):

        actual_value_for_equals = actual_value
        expected_value_for_equals = expected_value
        self.assertEqual(actual_value_for_equals,expected_value_for_equals)


# This is another unit test case which we have written named assert true that means this function will return require a Boolean value and depending upon the value of the that boolean variable the nature of the resultant test case is dertemined so in this function we are using the assert false functionality of the unit test case class to test the actual and expected output of a mathematical operations so we are passing the oeprand one as the actual value by actual value we mean the value which is actually calculated by the any of the above written mathematical operation let's say multiplication so the resultant of the multiplication function is passed as the first operand to this function and the second operand which is passed to this function is the expected value or which is meant to be the actual output of that function and both of these operations are compared and stored into a variable since the comparison is done on two operands return type is a Boolean value show the variable storing those values will be a Boolean variable and that Boolean variable will be passed to the assert false function if that  Boolean variable is having the true value then the test function will be passed on the other hand if the value is fasle the assert true unit test case will be failed
    def the unit test_for_assert_true(self,operand1,operand2):

        boolean_resultant = operand1 == operand2
        self.assertTrue(boolean_resultant)



# This is another unit test case which we have written named assert false that means this function will return require a Boolean value and depending upon the value of the that boolean variable the nature of the resultant test case is dertemined so in this function we are using the assert false functionality of the unit test case class to test the actual and expected output of a mathematical operations so we are passing the oeprand one as the actual value by actual value we mean the value which is actually calculated by the any of the above written mathematical operation let's say multiplication so the resultant of the multiplication function is passed as the first operand to this function and the second operand which is passed to this function is the expected value or which is meant to be the actual output of that function and both of these operations are compared and stored into a variable since the comparison is done on two operands return type is a Boolean value show the variable storing those values will be a Boolean variable and that Boolean variable will be passed to the assert false function if that  Boolean variable is having the fasle value then the test function will be passed on the other hand if the value is true the assert fasle unit test case will be failed
    def the unit test_for_assert_false(self,operand1,operand2):

        boolean_resultant = operand1 != operand2
        self.assertFalse(boolean_resultant)




#  In The End the main function is written,  which has the object of the above-written class Which is used to call all the methods which are written inside the class. the user is provided with the list of menus from which he has to select the mathematical operation which he performs on the two inputs which are going he is going to provide after selecting the appropriate mathematical operation the result of that mathematical operation is presented to the user and another menu is printed from which the type of unit test which needs to be Run on that obtained result is  shown,  on selecting the appropriate unit test which will run on the result of the mathematical operation that particular test case is Run and depending upon the unit test which is selected by the user appropriate message is shown,  that means if the test case is passed it is shown that that particular test case has been passed successfully on the other hand if the test case is failed due to some exception or error that has been encountered during the execution of that particular unit test case,  then that particular exception or error message is printed to the user and which line has caused that an exception message is also presented to the user this helps in the debugging so that user can understand by which particular line of code that particular unit test is failing this printing of the menu is done in a recursive manner until the user exits the code execution by selecting the last option which is to exit the code execution. 
def main():

    run_the unit test =  BasicMathOperationsTesting()

    while(True):

        # from the listed below the list of operations select any one of the operations
        print("Select any of the mathematical operations which are listed below:")
        print("1. To perform the addition operation and then perform the unit test on the result obtained.")
        print("2. To perform the subtraction operation and then perform the unit test on the result obtained.")
        print("3. To perform the multiplication operation and then perform the unit test on the result obtained.")
        print("4. To perform the division operation and then perform the unit test on the result obtained.")
        print("5. To exit from the code execution.")

        menu_choice = input()
        menu_choice = int(menu_choice)

        if menu_choice == 1:
            result = run_the unit test.perform_addition()
        elif menu_choice == 2:
            result = run_the unit test.perform_subtraction()
        elif menu_choice == 3:
            result = run_the unit test.perform_multiplication()
        elif menu_choice == 4:
            result = run_the unit test.perform_division()
        elif menu_choice == 5:
            sys.exit()

        print("Select any of the unit tests to perform which are listed below:")
        print("1. To perform the assertEqual the unit test on the above done mathematical operation.")
        print("2. To perform the assertTrue the unit test on the above done mathematical operation.")
        print("3. To perform the assertFalse the unit test on the above done mathematical operation.")

        menu_choice_for_unitttest = input()
        menu_choice_for_unitttest = int(menu_choice_for_unitttest)

        if menu_choice_for_unitttest == 1:
            print("Expected value for test to pass:")
            expected_value = int(input())
            run_the unit test.the unit test_for_assert_equals(result,expected_value)
        elif menu_choice_for_unitttest == 2:
            print("Expected value for test to pass:")
            expected_value = int(input())
            run_the unit test.the unit test_for_assert_true(result,expected_value)
        elif menu_choice_for_unitttest == 3:
            print("Expected value for test to pass:")
            expected_value = int(input())
            run_the unit test.the unit test_for_assert_false(result,expected_value)


        print("To go on with the code getting executed, enter input [y] or [n]")
        continue_or_exit = input()

        if continue_or_exit == 'y' or continue_or_exit == 'Y':
            pass
        elif continue_or_exit == 'n' or continue_or_exit == 'N':
            sys.exit()

if __name__ == '__main__':
    main()

输出:

Select any of the mathematical operations which are listed below:
1. To perform the addition operation and then perform the unit test on the result obtained.
2. To perform the subtraction operation and then perform the unit test on the result obtained.
3. To perform the multiplication operation and then perform the unit test on the result obtained.
4. To perform the division operation and then perform the unit test on the result obtained.
5. To exit from the code execution.
1
Enter the first number for the addition operation
99
Enter the second number for the addition operation
578
Select any of the unit tests to perform which are listed below:
1. To perform the assertEqual the unit test on the above done mathematical operation.
2. To perform the assertTrue the unit test on the above done mathematical operation.
3. To perform the assertFalse the unit test on the above done mathematical operation.
1
The expected value for the test to pass:
677
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
To go on with the code getting executed, enter input [y] or [n]
y
Select any of the mathematical operations which are listed below:
1. To perform the addition operation and then perform the unit test on the result obtained.
2. To perform the subtraction operation and then perform the unit test on the result obtained.
3. To perform the multiplication operation and then perform the unit test on the result obtained.
4. To perform the division operation and then perform the unit test on the result obtained.
5. To exit from the code execution.
1
Enter the first number for the addition operation
100
Enter the second number for the addition operation
544
Select any of the unit tests to perform which are listed below:
1. To perform the assertEqual the unit test on the above done mathematical operation.
2. To perform the assertTrue the unit test on the above done mathematical operation.
3. To perform the assertFalse the unit test on the above done mathematical operation.
1
The expected value for the test to pass:
647


self.assertEqual(actual_value_for_equals,expected_value_for_equals)
AssertionError: 647 != 677
----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (failures=1)


To go on with the code getting executed, enter input [y] or [n]
y
Select any of the mathematical operations which are listed below:
1. To perform the addition operation and then perform the unit test on the result obtained.
2. To perform the subtraction operation and then perform the unit test on the result obtained.
3. To perform the multiplication operation and then perform the unit test on the result obtained.
4. To perform the division operation and then perform the unit test on the result obtained.
5. To exit from the code execution.
2
Enter the first number for the subtraction operation
122
Enter the second number for the subtraction operation
22
Select any of the unit tests to perform which are listed below:
1. To perform the assertEqual the unit test on the above done mathematical operation.
2. To perform the assertTrue the unit test on the above mathematical operation.
3. To perform the assertFalse the unit test on the above done mathematical operation.
2
The expected value for the test to pass:
100
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
To go on with the code getting executed, enter input [y] or [n]
y
Select any of the mathematical operations which are listed below:
1. To perform the addition operation and then perform the unit test on the result obtained.
2. To perform the subtraction operation and then perform the unit test on the result obtained.
3. To perform the multiplication operation and then perform the unit test on the result obtained.
4. To perform the division operation and then perform the unit test on the result obtained.
5. To exit from the code execution.
2
Enter the first number for the subtraction operation
255
Enter the second number for the subtraction operation
50
Select any of the unit tests to perform which are listed below:
1. To perform the assertEqual the unit test on the above done mathematical operation.
2. To perform the assertTrue the unit test on the above done mathematical operation.
3. To perform the assertFalse the unit test on the above done mathematical operation.
2
The expected value for the test to pass:
200

self.assertTrue(boolean_resultant)
AssertionError: False
----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (failures=1)


To go on with the code getting executed, enter input [y] or [n]
y
Select any of the mathematical operations which are listed below:
1. To perform the addition operation and then perform the unit test on the result obtained.
2. To perform the subtraction operation and then perform the unit test on the result obtained.
3. To perform the multiplication operation and then perform the unit test on the result obtained.
4. To perform the division operation and then perform the unit test on the result obtained.
5. To exit from the code execution.
3
Enter the first number for the multiplication operation
100
Enter the second number for the multiplication operation
2
Select any of the unit tests to perform which are listed below:
1. To perform the assertEqual the unit test on the above done mathematical operation.
2. To perform the assertTrue the unit test on the above done mathematical operation.
3. To perform the assertFalse the unit test on the above done mathematical operation.
2
The expected value for the test to pass:
200
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

To go on with the code getting executed, enter input [y] or [n]
y
Select any of the mathematical operations which are listed below:
1. To perform the addition operation and then perform the unit test on the result obtained.
2. To perform the subtraction operation and then perform the unit test on the result obtained.
3. To perform the multiplication operation and then perform the unit test on the result obtained.
4. To perform the division operation and then perform the unit test on the result obtained.
5. To exit from the code execution.
3
Enter the first number for the multiplication operation
122
Enter the second number for the multiplication operation
654
Select any of the unit tests to perform which are listed below:
1. To perform the assertEqual the unit test on the above done mathematical operation.
2. To perform the assertTrue the unit test on the above done mathematical operation.
3. To perform the assertFalse the unit test on the above done mathematical operation.
2
The expected value for the test to pass:
55412

self.assertTrue(boolean_resultant)
AssertionError: False
----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (failures=1)

To go on with the code getting executed, enter input [y] or [n]
y
Select any of the mathematical operations which are listed below:
1. To perform the addition operation and then perform the unit test on the result obtained.
2. To perform the subtraction operation and then perform the unit test on the result obtained.
3. To perform the multiplication operation and then perform the unit test on the result obtained.
4. To perform the division operation and then perform the unit test on the result obtained.
5. To exit from the code execution.
4
Enter the first number for division operation
55
Enter the second number for division operation
5
Select any of the unit tests to perform which are listed below:
1. To perform the assertEqual the unit test on the above done mathematical operation.
2. To perform the assertTrue the unit test on the above done mathematical operation.
3. To perform the assertFalse the unit test on the above done mathematical operation.
3
The expected value for a test to pass:
12

----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
To go on with the code getting executed, enter input [y] or [n]
y
Select any of the mathematical operations which are listed below:
1. To perform the addition operation and then perform the unit test on the result obtained.
2. To perform the subtraction operation and then perform the unit test on the result obtained.
3. To perform the multiplication operation and then perform the unit test on the result obtained.
4. To perform the division operation and then perform the unit test on the result obtained.
5. To exit from the code execution.
4
Enter the first number for division operation
100
Enter the second number for division operation
10
Select any of the unit tests to perform which are listed below:
1. To perform the assertEqual the unit test on the above done mathematical operation.
2. To perform the assertTrue the unit test on the above done mathematical operation.
3. To perform the assertFalse the unit test on the above done mathematical operation.
3
The expected value for the test to pass:
10

self.assertFalse(boolean_resultant)
AssertionError: False
----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (failures=1)


To go on with the code getting executed, enter input [y] or [n]
y
Select any of the mathematical operations which are listed below:
1. To perform the addition operation and then perform the unit test on the result obtained.
2. To perform the subtraction operation and then perform the unit test on the result obtained.
3. To perform the multiplication operation and then perform the unit test on the result obtained.
4. To perform the division operation and then perform the unit test on the result obtained.
5. To exit from the code execution.
5

在上述的代码中,编写了主函数,该函数具有上述编写的类的对象,用于调用类内部编写的所有方法。用户提供了一个菜单列表,用户必须在选择适当的数学运算之后,在提供的两个输入上执行该数学运算。将该数学运算的结果呈现给用户,并打印另一个菜单,显示需要在获取的结果上运行的单元测试类型。选择适当的单元测试后,将运行数学运算结果的特定测试用例,并根据用户选择的单元测试显示适当的消息,也就是说,如果测试用例通过了,则显示该特定测试用例已成功通过;另一方面,如果测试用例由于某个异常或错误而失败,则向用户打印该特定异常或错误消息,并呈现引发该异常的行对用户进行调试有所帮助,因此用户可以了解到该特定单元测试失败的代码行。该菜单的打印以递归方式进行,直到用户选择退出代码执行为止,即选择最后一个选项即退出代码执行。

结论

因此,在本文中,我们已经看到如何使用Python的一些库来自动化软件测试过程,这有助于我们快速运行测试用例,帮助我们分析我们编写的代码的质量,以及该代码片段是否满足编写该代码的确切要求。除此之外,还有许多库可以用来自动化整个软件测试过程,我们将在其他文章中讨论它们。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程