mp_log_utils.py 1.1 KB
Newer Older
zhouxiang's avatar
zhouxiang committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import os

import allure
from pytest import assume


def write_log(config,
              result,
              msg,
              is_new: bool = True,
              case_path_tag: str = 'default'):
    try:
        log_path = os.path.join(config.get('log_path'), case_path_tag)

        if is_new:
            file = open(log_path, 'w')
        else:
            file = open(log_path, 'a')

        file.writelines('result:' + result + ', reason:' + msg + '\n')
        file.close()
    except Exception as e:
        return False, None, f'Unknown error: {e}'


def assert_log(config, case_path_tag: str = 'default'):
    log_path = os.path.join(config.get('log_path'), case_path_tag)

    with open(log_path, 'r') as f:
        lines = f.readlines()

        for line in lines:
            if 'result:False, reason:' in line:
                result = False
                msg = line
                break
            if 'result:True, reason:' in line and result is False:
                result = True

    allure.attach.file(log_path, attachment_type=allure.attachment_type.TEXT)
    with assume:
        assert result, msg