Technology Sharing

What is interface testing and how do we implement it?

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1. What is interface testing?
As the name implies, interface testing is to test the interfaces between systems or components, mainly to verify the data exchange, transmission and control management process, as well as the mutual logical dependencies. The interface protocols are divided into HTTP, WebService, Dubbo, Thrift, Socket and other types, and the test types are mainly divided into functional testing, performance testing, stability testing, security testing and so on.

In the "pyramid" model of layered testing, interface testing belongs to the second layer of service integration testing. Compared with UI layer (mainly WEB or APP) automated testing, interface automated testing has greater benefits, is easy to implement, has low maintenance costs, and has a higher input-output ratio. It is the first choice for every company to carry out automated testing.

Below we take an HTTP interface as an example to fully introduce the interface automation testing process: from requirements analysis to use case design, from script writing, test execution to result analysis, and provide complete use case design and test scripts.

2. Basic process
The basic interface function automation test process is as follows:

Requirements analysis -> Use case design -> Script development -> Test execution -> Result analysis

2.1 Example interface
Interface name: Douban movie search

Interface document address: https://developers.douban.com/wiki/?title=movie_v2#search

Interface call example:

1) Search by cast and crew: https://api.douban.com/v2/movie/search?q=张艺牟

2) Search by movie title: https://api.douban.com/v2/movie/search?q=A Chinese Odyssey

3) Search by genre: https://api.douban.com/v2/movie/search?tag=Comedy


3. Demand Analysis
Requirements analysis refers to the requirements, design and other documents. On the basis of understanding the requirements, it is also necessary to understand the internal implementation logic, and at this stage, you can point out the unreasonableness or omissions in the requirements and design.

For example, the Douban movie search interface, as I understand it, requires support for searching for movie titles, cast and crew, and tags, and returning search results in pages.

4. Use case design
Use case design is based on understanding the interface testing requirements, and uses mind mapping software such as MindManager or XMind to write test case designs. The main contents include parameter verification, function verification, business scenario verification, security and performance verification, etc. Common use case design methods include equivalence class partitioning method, boundary value analysis method, scenario analysis method, cause-and-effect diagram, orthogonal table, etc.

For the functional testing of Douban movie search interface, we mainly designed the test cases from three aspects: parameter verification, functional verification, and business scenario verification. The following are the test cases:

5. Script development

Based on the test case design written above, we used the python+nosetests framework to write the relevant automated test scripts, which can fully realize the interface automated testing, automatic execution and email test report functions.

5.1 Related lib installation

The necessary lib libraries are as follows, which can be installed using the pip command:

  1. pip install nose
  2. pip install nose-html-reporting
  3. pip install requests

5.2 Interface Call

Using the requests library, we can easily write the above interface calling method (such as searching for q=Andy Lau, the sample code is as follows):

  1. #coding=utf-8
  2. import requests
  3. import json
  4. url = 'https://api.douban.com/v2/movie/search'
  5. params=dict(q=u'刘德华')
  6. r = requests.get(url, params=params)
  7. print 'Search Params:n', json.dumps(params, ensure_ascii=False)
  8. print 'Search Response:n', json.dumps(r.json(), ensure_ascii=False, indent=4)

When writing automated test scripts, we need to do some encapsulation. In the following code, we encapsulate the Douban movie search interface. The test_q method only needs to use the yield method provided by nosetests to easily loop through each test set in the list qs:

  1. class test_doubanSearch(object):
  2. @staticmethod
  3. def search(params, expectNum=None):
  4. url = 'https://api.douban.com/v2/movie/search'
  5. r = requests.get(url, params=params)
  6. print 'Search Params:n', json.dumps(params, ensure_ascii=False)
  7. print 'Search Response:n', json.dumps(r.json(), ensure_ascii=False, indent=4)
  8. def test_q(self):
  9. # 校验搜索条件 q
  10. qs = [u'白夜追凶', u'大话西游', u'周星驰', u'张艺谋', u'周星驰,吴孟达', u'张艺谋,巩俐', u'周星驰,大话西游', u'白夜追凶,潘粤明']
  11. for q in qs:
  12. params = dict(q=q)
  13. f = partial(test_doubanSearch.search, params)
  14. f.description = json.dumps(params, ensure_ascii=False).encode('utf-8')
  15. yield (f,)

We design the test cases and write the automated test scripts for each function in turn.

5.3 Result Verification
When manually testing an interface, we need to determine whether the test has passed based on the results returned by the interface. The same is true for automated testing.

For this interface, we search for "q=Andy Lau", we need to determine whether the returned results contain "Cast member Andy Lau or film title Andy Lau", when searching for "tag=comedy", we need to determine whether the movie type in the returned results is "comedy", and when paging the results, we need to verify whether the number of returned results is correct, etc. The complete result verification code is as follows:

  1. class check_response():
  2. @staticmethod
  3. def check_result(response, params, expectNum=None):
  4. # 由于搜索结果存在模糊匹配的情况,这里简单处理只校验第一个返回结果的正确性
  5. if expectNum is not None:
  6. # 期望结果数目不为None时,只判断返回结果数目
  7. eq_(expectNum, len(response['subjects']), '{0}!={1}'.format(expectNum, len(response['subjects'])))
  8. else:
  9. if not response['subjects']:
  10. # 结果为空,直接返回失败
  11. assert False
  12. else:
  13. # 结果不为空,校验第一个结果
  14. subject = response['subjects'][0]
  15. # 先校验搜索条件tag
  16. if params.get('tag'):
  17. for word in params['tag'].split(','):
  18. genres = subject['genres']
  19. ok_(word in genres, 'Check {0} failed!'.format(word.encode('utf-8')))
  20. # 再校验搜索条件q
  21. elif params.get('q'):
  22. # 依次判断片名,导演或演员中是否含有搜索词,任意一个含有则返回成功
  23. for word in params['q'].split(','):
  24. title = [subject['title']]
  25. casts = [i['name'] for i in subject['casts']]
  26. directors = [i['name'] for i in subject['directors']]
  27. total = title + casts + directors
  28. ok_(any(word.lower() in i.lower() for i in total),
  29. 'Check {0} failed!'.format(word.encode('utf-8')))
  30. @staticmethod
  31. def check_pageSize(response):
  32. # 判断分页结果数目是否正确
  33. count = response.get('count')
  34. start = response.get('start')
  35. total = response.get('total')
  36. diff = total - start
  37. if diff >= count:
  38. expectPageSize = count
  39. elif count > diff > 0:
  40. expectPageSize = diff
  41. else:
  42. expectPageSize = 0
  43. eq_(expectPageSize, len(response['subjects']), '{0}!={1}'.format(expectPageSize, len(response['subjects'])))

5.4 Executing the test

For the above test script, we can use the nosetests command to easily run automated tests, and use the nose-html-reporting plug-in to generate HTML format test reports.

Run the command as follows:

nosetests -v test_doubanSearch.py:test_doubanSearch --with-html --html-report=TestReport.html

5.5 Sending Email Reports

After the test is completed, we can use the method provided by the smtplib module to send a test report in HTML format. The basic process is to read the test report -> add email content and attachments -> connect to the mail server -> send email -> exit. The sample code is as follows:

  1. import smtplib
  2. from email.mime.text import MIMEText
  3. from email.mime.multipart import MIMEMultipart
  4. def send_mail():
  5. # 读取测试报告内容
  6. with open(report_file, 'r') as f:
  7. content = f.read().decode('utf-8')
  8. msg = MIMEMultipart('mixed')
  9. # 添加邮件内容
  10. msg_html = MIMEText(content, 'html', 'utf-8')
  11. msg.attach(msg_html)
  12. # 添加附件
  13. msg_attachment = MIMEText(content, 'html', 'utf-8')
  14. msg_attachment["Content-Disposition"] = 'attachment; filename="{0}"'.format(report_file)
  15. msg.attach(msg_attachment)
  16. msg['Subject'] = mail_subjet
  17. msg['From'] = mail_user
  18. msg['To'] = ';'.join(mail_to)
  19. try:
  20. # 连接邮件服务器
  21. s = smtplib.SMTP(mail_host, 25)
  22. # 登陆
  23. s.login(mail_user, mail_pwd)
  24. # 发送邮件
  25. s.sendmail(mail_user, mail_to, msg.as_string())
  26. # 退出
  27. s.quit()
  28. except Exception as e:
  29. print "Exceptioin ", e

6. Results Analysis

Open the test report generated after nosetests is completed, you can see that a total of 51 test cases were executed in this test, 50 of which succeeded and 1 failed.

In the failed test case, we can see that the parameters passed in are: {"count": -10, "tag": "comedy"}. The number of results returned at this time is inconsistent with our expected result (when count is a negative number, the expected result is that the interface reports an error or uses the default value of 20, but the actual number of results returned is 189. Hurry up and report a bug to Douban- -)

7. Complete Script
I have uploaded the complete automated test script of Douban movie search interface to GitHub. Download address: test_demo/test_douban at master · lovesoo/test_demo · GitHub

After the download is complete, use the following command to perform a complete interface automation test and send the final test report via email:

python test_doubanSearch.py
Finally, the test report email is sent, the screenshot is as follows

Finally, I would like to thank everyone who read my article carefully. It is always necessary to exchange gifts. Although it is not something very valuable, you can take it directly if you need it:

These materials should be the most comprehensive and complete preparation warehouse for friends who are engaged in [software testing]. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can help you too!