Skip to content

Commit

Permalink
test of module, notebook and unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
toravest committed Mar 6, 2025
1 parent 0760588 commit 4f7dd7f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
54 changes: 54 additions & 0 deletions notebooks/test_notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hello world!'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import sys\n",
"import os\n",
"\n",
"# Gets the absolute path to the src folder\n",
"sys.path.append(os.path.abspath(\"../src\"))\n",
"\n",
"# Now we can import the fucntion from the module\n",
"from my_package.test_module import hello\n",
"\n",
"hello()\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Empty file added src/my_package/__init__.py
Empty file.
2 changes: 2 additions & 0 deletions src/my_package/test_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def hello():
return ("Hello World!")
21 changes: 21 additions & 0 deletions tests/unit/test_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import unittest
import sys, os

# This will make the absolute path, from the working directory, so it will not work every time, if we hav 'cd' into another folder
# sys.path.append(os.path.abspath("src"))

# This will make the absolute path from the root of the project, and will therefor work every time
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../../src")))

# ^This problem will only occure when trying to run the test using the terminal

from my_package import test_module

class TestTest(unittest.TestCase):
def test_hello(self):
expected_output = "Hello World!"
self.assertEqual(test_module.hello(), expected_output)


if __name__ == '__main__':
unittest.main()

0 comments on commit 4f7dd7f

Please sign in to comment.