0

I am relatively inexperienced in the field of Python. I am working with Python Requets and BeautifulSoup. The scripts I write with it are all executable.

Since I don't know anything about Pytest and Unittest, I tried to acquire this knowledge, but I just can't get any further.

I've seen in particular that Python Requests works over Mocks, but I have to say, I'm not sure this is the right way to write tests and include them in a Jenkins pipeline. The goal is to test different http requests of a website, either as a unit test or via Pytest.

What is your opinion?

Is that also possible with Python requests (integration) via mocks?

Do I have to use Pytest or Unittesting?

import requests 
from requests.exceptions import HTTPError
import pytest 
from bs4 import BeautifulSoup
import postpython

page = requests.get('http://google.de')
soup = BeautifulSoup(page.content, 'html.parser')
print(soup)
Mornon
  • 59
  • 5
  • 22

1 Answers1

0

Taken from here:

You have two types of tests:

  1. Integration - An integration test checks that components in your application operate with each other i.e. as a whole end-to-end without any mocks.
  2. Unit - A unit test checks a small component in your application in isolation i.e. mocking all the dependencies for that.

Suppose you have application code that makes a request to an API.

You would use unit test to check whether in your application code a request to the API is called or not (without actually making the API request). You would mock the behavior of sending request and plant a fake response in your unit test. This is so that you can test your code in isolation of the API request/response.

But, if you wanted to actually call the API and get back a real response, then you would use integration tests. This allows you to test the application behavior as a whole.

In python, unittest and pytest are two of many available test runner libraries for writing and managing such tests.

If you wanted to mock the requests/response then check this out: How can I mock requests and the response?

From what I understand, you're looking to actually make the request in your Jenkins CI/CD pipeline to the server and see if you get a successful response. This falls into the category of Integration Testing so you wouldn't need to mock anything.

You can do integration tests with both unittest or pytest.

Here's an example with pytest:

import pytest

def test_get_request():
    response = requests.get('http://google.de')
    assert response.status_code == 200

To run this you can run this command line task in your jenkins pipeline:

pytest your_test_file.py

You can follow this tutorial for more info and introduction on PyTest here.

Rithin Chalumuri
  • 1,739
  • 7
  • 19