Pytest断言错误是阻止执行的测试

0

的问题

我有以下代码读取数据,从外部excel和打员额的请求,但每当我遇到了一个断言的错误1行的数据设置,从excel,对于循环停止其余的excel行不采取执行。 该计划的执行得到停止在这一点上。 如何克服这个吗? 我的要求是,如果出的10个数据集(10行中的excel),断言失败在一个第5例如,它应该执行该程序与其他数据集,并在结束时标记考试的情况下执行失败。

def test_1A():

url_plan = getCOnfigurations()['API']['URL'] + APIResources.endpoints_plans
classic_sum_values = [200000, 300000, 400000]
payload_plan = open('C:/Users/aprat/PycharmProjects/RSAHealthPytest/PayLoads/plans/1A.json','r')
json_payload_plan = json.loads(payload_plan.read())

obj = Library.BaseClass("C:/Users/aprat/PycharmProjects/RSAHealthPytest/TestData/1A/TestData1A.xlsx","sheet1")
col = obj.fetch_column_count()
row = obj.fetch_row_count()
key_age = obj.fetch_key_names()
key_dob = obj.fetch_key_names()
key_sum = obj.fetch_key_names()
key_ten = obj.fetch_key_names()
key_pr_mem_age = obj.fetch_key_names()

for i in range(2, row+1):
    datadriven_json_data_plan = obj.update_request_with_data_plan1A(i,json_payload_plan,key_age,key_dob,key_pr_mem_age,key_sum,key_ten)
    a1_age = json_payload_plan['payload']['data']['insured'][0]['age']
    a1_dob = json_payload_plan['payload']['data']['insured'][0]['dob']
    prim_mem_age = json_payload_plan['payload']['data']['primary_member_age']
    tot_sum = json_payload_plan['payload']['data']['sum_insured']
    tenure = json_payload_plan['payload']['data']['tenure']
    response = requests.post(url_plan, json=datadriven_json_data_plan,)
    json_response_plan = response.json()
    print(json_response_plan)
    if int(tenure) == 1 or int(tenure) == 2 or int(tenure) == 3:
        if int(tot_sum) in classic_sum_values:
            if int(18) <= int(a1_age) <= int(50):
                print(json_response_plan[0]['article_code'])
                try:
                    assert int(json_response_plan[0]['premium']) > int(1)
                    assert int(json_response_plan[0]['term']) == int(tenure)
                    assert int(json_response_plan[0]['validity']) == int(tenure)
                    assert int(json_response_plan[0]['sa']) == int(tot_sum)
                except Exception as e:
                    json_data = datadriven_json_data_plan
                    sys.stdout = open("plans_classic_1A.log", "a")
                    _, _, tb = sys.exc_info()
                    traceback.print_tb(tb)
                    tb_info = traceback.extract_tb(tb)
                    filename, line, func, text = tb_info[-1]
                    print('An error occurred on line {} in statement {}'.format(line, text))
                    print("Test failure due to assertion error")
                    print("adult1 age=" + " " + str(a1_age))
                    print("sum insured=" + " " + str(tot_sum))
                    print("tenure=" + " " + str(tenure))
                    print(json.dumps(json_data, indent=2))
                    print("**************************************************************************")
                    raise e
api automation pytest python
2021-11-23 17:28:41
1

最好的答案

0

问题是,你提出的错误,从而允许其通过的循环。 删除 raise e 在结束你的代码和添加某种错误的处理/记录,以通过一份报告。

一个列表那你追加可能是最简单的解决方案,但使用的蟒蛇 logging 包裹是一个更好的长期解决办法。

编辑:根据以前的评论,设置一个标志,defaults to True/成功。 如果它一旦失败,这是自动设定假/失败。

例如:

def test1a():
    successFlag = True
...
    except Exception as e:
        successFlag = False

    return successFlag

2021-11-23 19:31:46

除了提高电子线路,但那时的整个试验的执行表示为通过在试验报告,该报告是一种误导。
Apratim Chaudhuri

增加一个编辑用的一个标志来追踪。
Alex D

这种做法没有工作了我。 在执行中,测试情况下状况表示为通过
Apratim Chaudhuri

没有在你的代码显示了在其中设置的通过/未通过功能,请提供更多的细节之外"这是失败的".
Alex D

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................