Master automated testing, 3 methods to easily implement timing execution!

When we are doing automation, when the automated test cases are written, we will think about automatically executing the program, or in other words, execute our automated test program in a scheduled form, so that we can truly realize automated testing. So do you know what methods can help us achieve this function? Next, the editor will briefly introduce how to execute our automated test case programs regularly in the next concentration.

Jenkins

When it comes to timing tasks, someone must think of Jenkins. Yes, Jenkins is a continuous integration tool, and there is a small function build timer in it, which can help us realize this function very well.

Instructions

By entering the corresponding test project, and then click configure (configuration) to enter its page.

picture

For Build Triggers on the configuration page, first check Build periodically, which indicates a scheduled build task, and the input box indicates the content of the rules that need to be built.

picture

A total of 5 * * * * * * can be written in the construction rule form, where each * needs to be separated by a space or a tab key:

  • The first * means minutes, the value is 0~59

  • The second * represents the hour, and the value is 0~23

  • The third * indicates the day of the month, the value is 1~31

  • The fourth * indicates which month, the value is 1~12

  • The fifth * indicates the day of the week, with a value of 0~7, where 0 and 7 represent the day of the week

Example: For example, H/5 * * * * once every 5 minutes.

After the build is complete, we wait for 5 minutes, check the task trigger, and find that the build is done every 5 minutes.

picture

schedule

schedule is a third-party library of Python, which can execute time according to seconds, minutes, hours, dates or custom time.

Installation: pip install schedule.

Instructions

The editor first introduces how to use this library, and then continues to introduce how to use the library through our automated test cases.

# coding:utf-8
import schedule
import time
def add():
    a = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    print('add函数每5秒运行一次::%s' %a)
    
def foo():
    b = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    print('foo函数每10秒运行一次::%s'%b)

# 通过schedule设置定时任务
schedule.every(5).seconds.do(add)  # 每5秒执行一次
schedule.every(10).seconds.do(foo)  # 每10秒执行一次

while True:
    schedule.run_pending() # 运行所有可运行的任务
    time.sleep(1)

By executing this script, you can see from the following print information that it has been run according to the rules we set.

picture

Of course, the schedule can not only set the second, but also can set the half-minute, hour, and week. I will not show it here, but only list the corresponding methods. Interested friends can try it by themselves.

schedule.every(10).minutes.do(执行程序函数名)   # 每10分钟执行一次
schedule.every().hour.do(执行程序函数名)   # 每小时执行一次
schedule.every().day.at("11:30").do(执行程序函数名) # 每天11点半执行
schedule.every().monday.do(执行程序函数名) # 每周一执行
schedule.every().wednesday.at("15:15").do(执行程序函数名) # 每周三15点15执

Automated test case

The editor here writes a few simple test cases, and then calls them through the schedule. In fact, the method is similar to the above method. Here, a method of passing in parameters through the schedule is added.

# coding:utf-8
import requests
url = 'http://apis.juhe.cn/simpleWeather/query'
def test_01():
    data = {
        "city": '上海',
        "key": 'xxxxxxxxx'
    }
    r = requests.post(url, data=data)
    assert r.json()['reason'] == '查询成功!'
def test_02():
    data = {
        "city": '北京',
        "key": 'xxxxxxxxx'
    }
    r = requests.post(url, data=data)
    assert r.json()['result']['city'] == '北京'

The editor here simply writes the interface automation test case, the test case is stored in the test_01.py file, we create a new execution file main.py file, and execute the program pytest program through the os module in the file.

# coding:utf-8
import schedule
import os
import time
def start(cmd):
    os.system(cmd)
    print('自动化测试用例执行完成:%s' % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

# 设置每5秒执行一次程序
schedule.every(5).seconds.do(start, 'pytest -vs test_01.py')

while True:
    schedule.run_pending()  # 运行所有可运行的任务
    time.sleep(1)

Through the command line, execute the corresponding program, and then you can see that our automated test case is executed normally every 5 seconds.

picture

task scheduler

The two methods have been briefly introduced above, and an operation about helping us realize timing tasks through Windows is introduced.

Instructions

Search for "Task Scheduler" in Windows:

picture

In the current page, right-click "Task Scheduler Library", and then enter into the create basic task wizard page, where you can set the name and description of our task for easy viewing.

Task Scheduler:

picture

After clicking Next, enter the trigger page, where we can set our trigger time, which can be set according to the project.

picture

After clicking Next, on the Daily page, you can set the time for the automatic running of the daily program and trigger once in a few days.

picture

The execution operation is to execute our program, here you can directly choose to execute the program.

picture

After clicking Next, enter the startup program, where we need to set the program we need to execute.

picture

After clicking Next, the setting is finally completed. We only need to check whether the information is correct, and then click Finish, so that our scheduled task has been set.

picture

Summarize

The editor introduces how to execute our automated test cases regularly through three methods. There may be some methods that you have used and some methods that you have not used, but you can understand it. Of course, the method of timing tasks here may be More than that, if there are other better methods, you can also leave a message to learn together.

Well, thank you very much for reading, I hope this article is helpful to you.

Finally: The complete software testing video tutorial below has been organized and uploaded, and friends who need it can get it by themselves [Guaranteed 100% free]

Software Testing Interview Documentation

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.

おすすめ

転載: blog.csdn.net/wx17343624830/article/details/131899361