简体   繁体   English

Python和pytest测试

[英]Python and pytest testing

I have to do some testing using pytest, but I have no idea where to start. 我必须使用pytest做一些测试,但是我不知道从哪里开始。 Here is piece of a code which i would like to test: 这是我要测试的一段代码:

def print_url_and_id():
    for item in movie_link[:100]:
        print item.contents[1], "The ID of this movie is:", '"' + item.contents[1]['href'][7:16] + '"'

Could anyone tell me how it suppose to look like? 谁能告诉我它的外观如何?

You can do something like this: 您可以执行以下操作:

import pytest


def parametrized():
    expected_results = ["movie_link_01", "movie_link_02"]
    movie_link = ["movie_link_01", "movie_link_02", "movie_link_03"]
    # you can define your new_movie_link list like this as you have done, but instead of
    # printing it, add it to a this new_movie_link list
    return [(item_1, item_2) for item_1, item_2 in zip(movie_link[:2], expected_results)]


@pytest.mark.parametrize("movie_link, expected", parametrized())
def test_parametrizer(movie_link, expected):
    assert movie_link == expected

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM