testing framework

This commit is contained in:
raf 2023-10-07 23:09:17 +03:00
parent 1c80a7508e
commit d2cfdd6d25
No known key found for this signature in database
GPG key ID: 02D1DD3FA08B6B29
2 changed files with 76 additions and 22 deletions

View file

@ -5,13 +5,12 @@ name: Python package
on:
push:
branches: [ "main" ]
branches: ["main"]
pull_request:
branches: [ "main" ]
branches: ["main"]
jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
@ -19,22 +18,26 @@ jobs:
python-version: ["3.10", "3.11"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
- name: Checkout
uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest

51
test_main.py Normal file
View file

@ -0,0 +1,51 @@
import unittest
from projectile.simulation import ProjectileSimulation
import numpy as np
class TestProjectileSimulation(unittest.TestCase):
def setUp(self):
self.sim = ProjectileSimulation()
def test_reset(self):
self.sim.reset()
self.assertEqual(self.sim.x, [0])
self.assertEqual(self.sim.y, [self.sim.h_init])
self.assertEqual(self.sim.vx, [self.sim.v_init * np.cos(self.sim.theta)])
self.assertEqual(self.sim.vy, [self.sim.v_init * np.sin(self.sim.theta)])
self.assertEqual(self.sim.t, [0])
def test_calculate_acceleration(self):
state = [
0,
self.sim.h_init,
self.sim.v_init * np.cos(self.sim.theta),
self.sim.v_init * np.sin(self.sim.theta),
]
acceleration = self.sim.calculate_acceleration(0, state)
self.assertEqual(len(acceleration), 4)
def test_update_state(self):
state = [
0,
self.sim.h_init,
self.sim.v_init * np.cos(self.sim.theta),
self.sim.v_init * np.sin(self.sim.theta),
]
new_state = self.sim.update_state(0, state)
self.assertEqual(len(new_state), 4)
def test_run(self):
self.sim.run()
self.assertTrue(self.sim.t[-1] >= self.sim.t_end)
def test_print_range(self):
self.sim.run()
range_projectile = self.sim.x[-1]
self.assertEqual(
self.sim.print_range(), f"Range of projectile: {range_projectile:.2f} m"
)
if __name__ == "__main__":
unittest.main()