使用 Python 自动化软件测试2024年8月29日 | 23 分钟阅读 软件测试是发现已创建产品中错误的过程。此外,它评估实际结果是否与预期结果一致,并有助于识别错误、缺失的需求或缺陷。测试是产品投放市场前的最后一步。它涉及查看、研究、观察和判断各种产品组件。行业中的软件测试人员结合了人工和自动化测试方法。运行测试后,测试人员将结果告知开发团队。软件测试至关重要,因为最终目标是为用户提供高质量的产品。 许多企业家忽略测试他们的产品。他们可能声称他们的财务限制是跳过这个关键阶段的原因。他们认为影响很小。然而,它必须是优秀的,才能建立一个强大而良好的第一印象。这需要对产品进行彻底的bug测试。 提高产品质量只有当所提供的产品是完美的,公司才能为其客户提供价值。为此,企业必须确保用户在使用其产品时没有问题。让您的产品无bug是实现这一目标的万无一失的方法。 在交付产品之前,公司必须专注于测试应用程序并修复在测试过程中发现的任何缺陷。当团队在产品到达消费者之前修复问题时,输出质量会提高。 提高安全性客户在使用产品时一定会泄露某种个人信息。在程序部署之前,需要进行安全测试,以防止黑客访问此信息。当公司遵循彻底的测试程序时,它能确保产品的安全,这有助于客户在使用产品时感到舒适。 客户不再只在大台式电脑上工作。在当今移动优先的世界中,测试产品的设备兼容性至关重要。假设您的公司创建了一个网站。测试人员必须查看网页是否在各种设备分辨率下工作。它还应该与各种浏览器兼容。浏览器选择的增加是测试越来越重要的另一个原因。适用于Chrome的可能不兼容Safari或Internet Explorer。这需要跨浏览器测试,其中涉及验证应用程序在多个浏览器上的互操作性。 软件测试的分类软件测试是一个广义术语,涵盖了各种活动。相反,它以多种形式出现,可以根据许多因素进行分类。例如,您可以将测试分为手动和自动化类别。在自动化测试方面,您可以选择基于代码和无代码方法,以及结合两者优点的混合方法。测试还可以根据它们对测试系统内部实现了解多少来分类。我们可以根据这个标准将测试分为三类:白盒、黑盒和灰盒。最后,我们可以根据它们是否确认应用程序的业务需求将测试分为功能性和非功能性类别。 黑盒测试 在这种测试中,您对产品开发方式的了解最少。您对产品的结构、编码或逻辑一无所知。您会像客户一样使用该产品。黑盒测试用于功能测试,因为您拥有与客户相同的信息量。这种形式的测试只有在代码运行后才能进行。因此,采用动态测试。动态测试是一种您必须运行代码并在产品运行时对其进行测试的测试。它主要是为了查看产品启动并运行后的工作方式,以及用户将如何对其做出反应。 灰盒测试 在这种测试中,您对产品只有有限的了解。这种形式的测试对于检测用户不会意识到的问题很有用。 举个例子,假设您创建了一个蓝色阴影的元素,但它实际上是绿色阴影。用户不会意识到存在缺陷,因为他们会认为这应该是这样的。然而,您对产品的有限了解将帮助您检测此类缺陷。 大猩猩测试大猩猩测试是一种软件测试方法,其中一个模块通过各种随机输入反复测试,以确保其无错误。只有系统中的少数选定模块经过测试,目的是评估模块是否正常运行。这种形式的测试是手动进行的,并且频繁进行。大猩猩测试也被称为折磨测试、容错测试和不愉快测试。 既然您已经了解了测试的全部内容,是时候学习如何进行软件测试了。
软件测试的好处
现在让我们看看Python编程语言中软件测试自动化示例之一的代码。 代码输出 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中的一些库来自动化软件测试过程,这有助于我们快速运行测试用例,从而帮助我们分析所编写代码的质量,并确保该特定代码片段满足编写此代码的精确要求。除了这些,还有许多其他库可以用于自动化整个软件测试过程,我们将在其他文章中讨论它们。 下一个主题使用Python自动化Google搜索 |
我们请求您订阅我们的新闻通讯以获取最新更新。