व्यावहारिक उदाहरण

Python में Testing का परिचय

Alexander Levin

Data Scientist

डेटा और पाइपलाइन

डेटा: डेटा साइंस में सैलरीज़.

हर पंक्ति में किसी डेटा साइंस प्रोफेशनल की सैलरी, टाइटल और अन्य गुण दिए हैं.

ds salaries table

पाइपलाइन: औसत सैलरी पाने के लिए:

  1. डेटा पढ़ें
  2. employment type से फ़िल्टर करें
  3. औसत सैलरी निकालें
  4. परिणाम सेव करें
Python में Testing का परिचय

पाइपलाइन का कोड

import pandas as pd

# Fixture to get the data
@pytest.fixture
def read_df():
    return pd.read_csv('ds_salaries.csv')
# Function to filter the data
def filter_df(df):
    return df[df['employment_type'] == 'FT']
# Function to get the mean
def get_mean(df):   
    return df['salary_in_usd'].mean()
Python में Testing का परिचय

इंटीग्रेशन टेस्ट्स

टेस्ट केस:

  • डेटा पढ़ना
  • फ़ाइल में लिखना

कोड:

def test_read_df(read_df):
    # Check the type of the dataframe
    assert isinstance(read_df, pd.DataFrame)
    # Check that df contains rows
    assert read_df.shape[0] > 0
Python में Testing का परिचय

इंटीग्रेशन टेस्ट्स

यह जाँचने का उदाहरण कि Python फ़ाइलें बना सकता है.

def test_write():
    # Opening a file in writing mode
    with open('temp.txt', 'w') as wfile:
        # Writing the text to the file
        wfile.write('Testing stuff is awesome')
    # Checking the file exists
    assert os.path.exists('temp.txt')
    # Don't forget to clean after yourself
    os.remove('temp.txt')
Python में Testing का परिचय

यूनिट टेस्ट्स

टेस्ट केस:

  • फ़िल्टर किया गया डेटासेट सिर्फ 'FT' employment type रखता है
  • get_mean() फंक्शन एक संख्या लौटाता है

कोड:

def test_units(read_df):
    filtered = filter_df(read_df)
    assert filtered['employment_type'].unique() == ['FT']
    assert isinstance(get_mean(filtered), float)
Python में Testing का परिचय

फ़ीचर टेस्ट्स

टेस्ट केस:

  • mean शून्य से बड़ा है
  • mean डेटासेट की अधिकतम सैलरी से बड़ा नहीं है

कोड:

def test_feature(read_df):
    # Filtering the data
    filtered = filter_df(read_df)
    # Test case: mean is greater than zero
    assert get_mean(filtered) > 0
    # Test case: mean is not bigger than the maximum
    assert get_mean(filtered) <= read_df['salary_in_usd'].max()
Python में Testing का परिचय

परफॉर्मेंस टेस्ट्स

टेस्ट केस:

  • पाइपलाइन का स्टार्ट से एंड तक execution time

कोड:

def test_performance(benchmark, read_df):
    # Benchmark decorator
    @benchmark
    # Function to measure
    def get_result():
        filtered = filter_df(read_df)
        return get_mean(filtered)
Python में Testing का परिचय

अंतिम टेस्ट सूट

import pytest

## Integration Tests
def test_read_df(read_df):
      # Check the type of the dataframe
    assert isinstance(read_df, pd.DataFrame)
    # Check that df contains rows
    assert read_df.shape[0] > 0
def test_write():
    with open('temp.txt', 'w') as wfile:
        wfile.write('12345')
    assert os.path.exists('temp.txt')
    os.remove('temp.txt')

## Unit Tests
def test_units(read_df):
    filtered = filter_df(read_df)
    assert filtered['employment_type'].unique() == ['FT']
    assert isinstance(get_mean(filtered), float)
## Feature Tests
def test_feature(read_df):
    # Filtering the data
    filtered = filter_df(read_df)
    # Test case: mean is greater than zero
    assert get_mean(filtered) > 0
    # Test case: mean is not bigger than the maximum
    assert get_mean(filtered) <= read_df['salary_in_usd'].max()

## Performance Tests
def test_performance(benchmark, read_df):
    # Benchmark decorator
    @benchmark
    # Function to measure
    def pipeline():
        filtered = filter_df(read_df)
        return get_mean(filtered)
Python में Testing का परिचय

अभ्यास करते हैं!

Python में Testing का परिचय

Preparing Video For Download...