Skip to content

Commit

Permalink
adding code_testing section
Browse files Browse the repository at this point in the history
  • Loading branch information
skfegan committed Aug 30, 2024
1 parent 242f5f8 commit 7469428
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
24 changes: 24 additions & 0 deletions code_testing/1_code_testing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Code Testing

One of the most important factors for a scientific code is being correct.
We want to be able to check that the code is giving the answer we expect and that any changes to the code are not inadvertantly changing the output.
This is why tests are a part of high quality code.

# Types of Tests

Unit tests are small tests that each test a particullar part (or unit) of the code.
Unit tests are used to test each function one at a time, and in some cases might even test just part of a function or class.

Integration tests check that pieces of code are working together as expected.
This is used for multiple functions at the same time.

Regression tests look at the final answer for an example input and are used to check that changes to one part of the code are not changing the overall performance.

# Code Coverage

Code coverage is a metric related to how much of your code is being tested.
Low code coverage suggests that there are parts of the code that may or may not contain bugs.
In this case, writting more tests is recommended.

High code coverage is good, but does not guarentee that all bugs have been found.
Some functions might need more than one test to cover all the possible input cases.
9 changes: 9 additions & 0 deletions code_testing/1_pytest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## Pytest

Pytest is one example of a program used for running tests.
The documentation for pytest can be found [here](https://docs.pytest.org)

*Activity* Change to the code_testing directory of the software workshop.
Look at the functions.py code and the test_functions.py.
Try running the test. Does it pass? if not why not and can you fix the code so that the test passes?
Does the test cover all the functionality? Try writting more tests.
Empty file added code_testing/__init__.py
Empty file.
15 changes: 11 additions & 4 deletions code_testing/functions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import numpy as nmp

def frequency_calculation(lamdas, temp):
def frequency_calculation(lamda):
"""
Calculate the frequency based on the equation
frequency = 1/(2pi) * sqrt(x)
"""
pi = nmp.pi
kt = 4.11e-21 * temp / 298
frequencies = 1/(2*pi) * nmp.sqrt(lamdas/kt)

# calculate the frequency
frequency = 1/(pi) * nmp.sqrt(lamda)

return frequency


return frequencies
12 changes: 12 additions & 0 deletions code_testing/test_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from code_testing import functions as fun

def test_frequency_zero():
lamda = 0

assert fun.frequency_calculation(lamda) == 0

def test_frequency_int():
lamda = 2

assert fun.frequency_calculation(lamda) == 0.22507907903927654

0 comments on commit 7469428

Please sign in to comment.