Skip to content

Commit

Permalink
playing with unint tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiocat93 committed Sep 17, 2024
1 parent 520c1e7 commit c3d8560
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/guide_notebooks_regression_ec2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,27 @@ jobs:
name: Do the job on the runner
needs: start-runner # required to start the main job when the runner is ready
runs-on: ${{ needs.start-runner.outputs.label }} # run the job on the newly created runner
env:
RUN_GPU_TESTS: true
steps:
- name: Hello World
run: echo 'Hello World!'
- uses: actions/checkout@v4
with: # no need for the history
fetch-depth: 1
- name: Set up Python 3.10
uses: actions/setup-python@v5
with:
python-version: 3.10
- name: Install dependencies
run: |
pip install pytest
shell: bash
- name: Run unit tests
id: run-tests
run: >
pytest
shell: bash

stop-runner:
name: Stop self-hosted EC2 runner
Expand Down
20 changes: 20 additions & 0 deletions math_operations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def basic_math_operation(x, y, operation):
"""Performs a basic math operation on two numbers.
:param x: First number
:param y: Second number
:param operation: Operation to perform: 'add', 'subtract', 'multiply', or 'divide'
:return: Result of the operation
"""
if operation == 'add':
return x + y
elif operation == 'subtract':
return x - y
elif operation == 'multiply':
return x * y
elif operation == 'divide':
if y == 0:
raise ValueError("Cannot divide by zero")
return x / y
else:
raise ValueError("Unsupported operation")
30 changes: 30 additions & 0 deletions test_math_operations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest
import os
from math_operations import basic_math_operation

def test_add():
assert basic_math_operation(2, 3, 'add') == 5
assert basic_math_operation(-2, 3, 'add') == 1
assert basic_math_operation(0, 0, 'add') == 0

def test_subtract():
assert basic_math_operation(5, 3, 'subtract') == 2
assert basic_math_operation(2, 5, 'subtract') == -3
assert basic_math_operation(0, 0, 'subtract') == 0

if os.getenv("RUN_GPU_TESTS") != "true":
def test_multiply():
assert basic_math_operation(2, 3, 'multiply') == 6
assert basic_math_operation(-2, 3, 'multiply') == -6
assert basic_math_operation(0, 5, 'multiply') == 0

def test_divide():
assert basic_math_operation(6, 3, 'divide') == 2
assert basic_math_operation(7, 2, 'divide') == 3.5

with pytest.raises(ValueError):
basic_math_operation(6, 0, 'divide')

def test_invalid_operation():
with pytest.raises(ValueError):
basic_math_operation(2, 3, 'modulus')

0 comments on commit c3d8560

Please sign in to comment.