Python automated testing - Python automated framework and tools

TEST_ Two black 2022-06-23 16:43:54 阅读数:927


1 summary

Continued methodology on testing , It's all based on the ideas mentioned in the previous article :

  • Functional testing is not recommended to be automated
  • Interface testing is the most cost-effective
  • Interface testing can be automated
  • What I'll talk about later test automation It will also revolve around Interface automation To introduce .

The test language chosen for this series is python Scripting language . Because its official documents have a clear explanation of the principle , This article will not do some redundant translation work . Prefer the actual combat part , And in order to favor actual combat , It will also combine with IDE Tools and project organization to explain .

For the following reasons :

1. Scripting language , Development and iteration are extremely efficient

2. There are many third-party extension Libraries , I have very ready-made tools to use

Before officially entering automated testing Before the field of , We must first establish such values . stay Google Software testing publications issued by internal engineers mention :

“ Automated testing of software is costly , And the cost is not low , Basically equivalent to the original Functional development engineering And then build a parallel Test development engineering ”.

in other words , If you have your expectations for automated testing , Then we must pay the corresponding price and energy . Good things also need good people to spend a lot of time to complete .

2 PyUnit The test framework

Use python As an automation programming language , Then use it naturally pyunit As an automated testing framework .

The following parts are mainly from pyunit Official documents of , This paper only makes some simple adjustments in translation and structure . This part belongs to the basic principles and concepts of the test framework , Before coding , It is necessary to understand .

python The unit test framework of PyUnit, Think of it as Java Unit test framework in language JUnit Of Python Language implementation version , Even one of its authors Kent Beck Namely JUnit The author of .

unittest To achieve the following goals :

  • Support for automated testing
  • Let all test scripts share Turn on (setup) and close (shutdown) Code for
  • It can be done by assembling (collections) To organize test case scripts
  • Separate all test scripts from the test report framework

In order to achieve the above goals ,unittest Support the following important concepts :

  • Test device (test fixture)
    Do some preparatory work for one or more test cases , for example : Connect to a database , Create a directory , Or start a process
  • The test case (test case)
    Test cases are the smallest unit of test behavior , Test and check by comparing some input and output values
  • test suite (test suite)
    take The test case perhaps Test case collection A collection organized by aggregation . All test cases in a test suite can be executed in batches
  • Test the actuator (test runner)
    Organize and arrange the components of test script execution activities . The test actuator passes through some graphical interfaces , Text interface or return some special values to show the test results of the test script . It is mainly used to generate test reports

3 Basic example

The following example is also from official documents

# coding:utf-8
""" Basic automated test scripts """
__author__ = 'zheng'
import unittest
class TestStringMethods(unittest.TestCase):
def setUp(self):
print 'init by setUp...'
def tearDown(self):
print 'end by tearDown...'
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
if __name__ == '__main__':

Although the official document describes several ways to organize test case scripts :

1. Independent test function
2. Single use case test class
3. Multi use case test class

Different writing forms , There will be different ways of organizing , See the official documents for details . After studying the official documents , I like the third way best Multi use case test class , That is the way of the basic example above , This method has the following characteristics :

  • Test class Inherited from unittest.TestCase
  • A test class can manage multiple Test script function
  • The test script function name needs to be marked with test_ start
  • All test functions in a test class are shared setUp and tearDown function

Run this program in the console :

* src git:(master) * python
init by setUp...
Fend by tearDown...
init by setUp...
end by tearDown...
.init by setUp...
end by tearDown...
FAIL: test_isupper (__main__.TestStringMethods)
Traceback (most recent call last):
File "", line 24, in test_isupper
AssertionError: False is not true
Ran 3 tests in 0.001s
FAILED (failures=1)
* src git:(master) *

The previous basic example main The simplest way to use a function , Run all test cases directly , And generate a default text report . In fact, you only need to make some simple modifications to the calling function , These test cases can be reasonably organized , And get useful data information , In order to integrate with the information system , Form a better expansion .

if __name__ == '__main__':
# unittest.main()
# Load test cases
test_cases = unittest.TestLoader().loadTestsFromTestCase(TestStringMethods)
# Use test suites and package test cases
test_suit = unittest.TestSuite()
# Run the test suite , And return the test results
test_result = unittest.TextTestRunner(verbosity=2).run(test_suit)
# Generate test reports
print("testsRun:%s" % test_result.testsRun)
print("failures:%s" % len(test_result.failures))
print("errors:%s" % len(test_result.errors))
print("skipped:%s" % len(test_result.skipped))

The generated output is the output after running :

* src git:(master) * python
test_isupper (__main__.TestStringMethods) ... init by setUp...
end by tearDown...
test_split (__main__.TestStringMethods) ... init by setUp...
end by tearDown...
test_upper (__main__.TestStringMethods) ... init by setUp...
end by tearDown...
FAIL: test_isupper (__main__.TestStringMethods)
Traceback (most recent call last):
File "", line 23, in test_isupper
AssertionError: False is not true
Ran 3 tests in 0.001s
FAILED (failures=1)

Obviously, the above input results have counted the test results , These data are important indicators in a test activity , These data can be put into storage , Integration with test information management system , Generate dashboard or statistical report later , Form stability and product test circuit diagram , These are all related to development , There is no more description here .

Combined with the above specific examples , We can also find the specific implementation object corresponding to the theoretical part of the previous section :

  • Test device (test fixture)
    from setUp Function to do initialization , from tearDown Do the destruction work

  • The test case (test case)
    Corresponding TestCase class , Or more detailed corresponding test script functions

  • test suite (test suite)
    Corresponding TestSuite class

  • Test the actuator (test runner)
    Corresponding TextTestRunner class

4 IDE Tools

Since you need to develop code productivity , Then you need to introduce a IDE Tools -- Pycharm. Undeniable? , It is currently the most focused / Professional Python Linguistic IDE 了 . In the face of Pyunit There is also good support .

The main support is as follows :

  • Visual programming development ( This is a IDE The basic characteristics of )

  • Visual display of test results

  • Export build HTML The test report of

  • Visual control case execution ( This is very convenient in the development and debugging stage , It is convenient to control the operation of specified code units )

1. Let all the files in a directory execute with their lives

2. Let all use cases in a single file execute

3. Let a single in a single file execute

4.1 Operation and commissioning

Pycharm It provides flexible running and debugging support for test scripts .

adopt pycharm, Developers don't have to write main function , The following functions can be realized :

  • Run all test classes under one file
  • Run all test scripts for a test class
  • Run a test script of a test class

among “ Run a test script of a test class ” More useful , It is suitable for rapid development, running and debugging of a single script in the development stage .

Usage method :

1. Move the cursor inside the test function

2. Press the run shortcut key ctrl+shift+F10 (Eclipse Shortcut scheme )

If you want to debug at breakpoint , Then use Debug Pattern , You can run a single function and debug breakpoints .

Of course , You don't have to borrow IDE, And by right of testSuit operation , It can also realize the above functions , however IDE But it provides a more flexible and direct choice . It's just some IDE Using skills , I won't say more .

4.2 Result visualization

For the example mentioned above , If you choose IDE Run this program in , You will see the following effect :
 Insert picture description here
You can see all the running through . If you deliberately make one of them fail , The following results will be displayed :
 Insert picture description here
4.3 Generate test reports

Pycharm It also provides the function of exporting test result reports , On a function button on the test result display box .

 Insert picture description here
The results are as follows :
 Insert picture description here
Of course , If integration with information system is not considered , The subsequent dashboard and test statistics are not considered , Just to generate a report , This function is enough .

In general , Develop and automate tests , The above skills can fully meet the requirements , The next thing to do is to use all kinds of basic computer knowledge , In the face of increasing business demand , And constantly add test case scripts .

Functional development project , The principle is very simple , But as the volume increases , Will form a scale , The same goes for test development engineering .

5 Project organization

Previous on test cases Development and debugging status The tools are introduced . But if you really want to include Continuous integration Automation system of , Obviously, we can't rely on IDE 了 . But use python The organization and invocation of language , such as : Want to have main Function as the execution entry , wait .

Detailed technical implementation details , There's a chance in the back , I'll write a corresponding article again to introduce .

Through disengagement IDE Project organization , It has the following advantages :

  • All scripts can be executed by event triggering ( Can become Continuous integration Part of the assembly line
  • All data can be put forward and customized processing and processing ( Integration with test information system , Provide data source for quality analysis system )

6 Test platform

About how to automatically generate the test report, this test product , Now some platforms can provide interface call, report display and sharing functions

 Insert picture description here

7 Summary

The content of this section , Mainly based on python Linguistic Automated testing framework pyunit Some design ideas and basic use examples of . In fact, the use of tools is very simple , But how to make good use of these tools to produce software , Other computer skills are needed , In the following articles, the application of this framework will be further extended from the engineering and technical aspects .

Learning resource sharing

Finally, thank everyone who reads my article carefully , Watching the rise and attention of fans all the way , Reciprocity is always necessary , Although it's not very valuable , If you can use it, you can take it

 Insert picture description here
These materials , For doing 【 software test 】 For our friends, it should be the most comprehensive and complete war preparation warehouse , This warehouse also accompanied me through the most difficult journey , I hope it can help you ! Everything should be done as soon as possible , Especially in the technology industry , We must improve our technical skills . I hope that's helpful …….

版权声明:本文为[TEST_ Two black]所创,转载请带上原文链接,感谢。