Random URL Getter and Status Checker

  • Share this:

Code introduction


This function randomly selects a URL from a predefined list of URLs, sends a GET request to that URL, and asserts that the response status code is 200. If the assertion passes, it returns the response text.


Technology Stack : Python, requests, pytest

Code Type : Python Function

Code Difficulty : Intermediate


                
                    
def random_choice(arg1, arg2, arg3):
    import random
    import pytest
    import requests

    # Generate a random URL from a list of URLs
    urls = ["http://example.com", "http://test.com", "http://random.com"]
    random_url = random.choice(urls)

    # Send a GET request to the random URL
    response = requests.get(random_url)

    # Assert that the response status code is 200
    assert response.status_code == 200

    # Extract the response text and return it
    return response.text