Home > OS >  pytest-ing a function with no arguments
pytest-ing a function with no arguments

Time:12-28

I am trying to complete a popular online python course. For the final project, I am required to test functions with pytest. However, the functions I have are simple, and the inputs are error checked before they are put in the class object Prime_Line. Is there any way to run pytest on a function such as below without re-writing a whole bunch of robust, working, code just so I can run a test on it?

Ex:

def extrusion_calculation():
    """Calculates extrusion number

    Returns:
        e (float): extrusion distance
    """
    D = Prime_Line.nozzle_diameter
    W = Prime_Line.line_width_factor * D
    T = Prime_Line.layer_height
    L = Prime_Line.line_length

    e = ((math.pi * T**2) / 4   T * W - T**2) * L
    return e

CodePudding user response:

You can use a dummy Prime_Line object in a test module using the Mock module. In code should be something like that:

import pytest
from unittest.mock import Mock
import math

def test_extrusion_calculation():
    # Create a mock object for Prime_Line
    Prime_Line = Mock()

    # Set the values for the mock object's attributes
    Prime_Line.nozzle_diameter = 0.4
    Prime_Line.line_width_factor = 0.7
    Prime_Line.layer_height = 0.2
    Prime_Line.line_length = 10

    # Call the function and assert the result is what you expect
    assert extrusion_calculation() == 4.76
  • Related