单元测试请求重试python

时间:2018-02-01 17:48:31

标签: python unit-testing

如果端点在返回结果之前超时,我尝试重试请求几次。这是代码:

def retry_request(self, params, max_retries=3):
    for i in xrange(max_retries):
        try:
            response = requests.get(params)
            break
        except requests.exceptions.Timeout as e:
            raise e

我想对重试进行单元测试以显示重试逻辑的工作原理。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

请求库具有其自己的重试逻辑。

尝试这样的事情:

library(tidyverse)
df <- tibble::tribble(
  ~STATE,      ~MONTH,      ~YEAR,   ~VALUE,
"California",     "JAN",      2018,      800,
"California",     "FEB",      2018,      780,
"California",     "MAR",      2018,      600,
"Minesota",       "JAN",      2018,      800,
"Minesota",       "FEB",      2018,      780,
"Minesota",       "MAR",      2018,      600)

df %>%
   tidyr::unite(TIME, c(MONTH, YEAR), sep = "-") %>%
   dplyr::mutate(TIME = lubridate::parse_date_time(TIME, "my"))
#> # A tibble: 6 x 3
#>   STATE      TIME                VALUE
#>   <chr>      <dttm>              <dbl>
#> 1 California 2018-01-01 00:00:00   800
#> 2 California 2018-02-01 00:00:00   780
#> 3 California 2018-03-01 00:00:00   600
#> 4 Minesota   2018-01-01 00:00:00   800
#> 5 Minesota   2018-02-01 00:00:00   780
#> 6 Minesota   2018-03-01 00:00:00   600