Continued methodology on testing , It's all based on the ideas mentioned in the previous article :
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 .
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 :
In order to achieve the above goals ,unittest Support the following important concepts :
The following example is also from official documents basic_demo.py:
# coding:utf-8
""" Basic automated test scripts basic_demo.py """
__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):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
self.assertTrue('Foo'.isupper())
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):
s.split(2)
if __name__ == '__main__':
unittest.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 :
Run this program in the console :
* src git:(master) * python basic_demo.py
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 "basic_demo.py", line 24, in test_isupper
self.assertTrue('Foo'.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()
test_suit.addTests(test_cases)
# 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 basic_demo.py
test_isupper (__main__.TestStringMethods) ... init by setUp...
FAIL
end by tearDown...
test_split (__main__.TestStringMethods) ... init by setUp...
end by tearDown...
ok
test_upper (__main__.TestStringMethods) ... init by setUp...
end by tearDown...
ok
======================================================================
FAIL: test_isupper (__main__.TestStringMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
File "basic_demo.py", line 23, in test_isupper
self.assertTrue('Foo'.isupper())
AssertionError: False is not true
----------------------------------------------------------------------
Ran 3 tests in 0.001s
FAILED (failures=1)
testsRun:3
failures:1
errors:0
skipped:0
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
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 :
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 :
You can see all the running through . If you deliberately make one of them fail , The following results will be displayed :
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 .
The results are as follows :
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 .
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 :
About how to automatically generate the test report, this test product , Now some platforms can provide interface call, report display and sharing functions
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 .
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
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 …….