Skip to content

Commit 2edcc94

Browse files
committed
write doctest & unittest
1 parent 590b380 commit 2edcc94

5 files changed

Lines changed: 248 additions & 0 deletions

File tree

.gitignore

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
2+
# Created by https://www.toptal.com/developers/gitignore/api/python
3+
# Edit at https://www.toptal.com/developers/gitignore?templates=python
4+
5+
.idea
6+
### Python ###
7+
# Byte-compiled / optimized / DLL files
8+
__pycache__/
9+
*.py[cod]
10+
*$py.class
11+
12+
# C extensions
13+
*.so
14+
15+
# Distribution / packaging
16+
.Python
17+
build/
18+
develop-eggs/
19+
dist/
20+
downloads/
21+
eggs/
22+
.eggs/
23+
parts/
24+
sdist/
25+
var/
26+
wheels/
27+
pip-wheel-metadata/
28+
share/python-wheels/
29+
*.egg-info/
30+
.installed.cfg
31+
*.egg
32+
MANIFEST
33+
34+
# PyInstaller
35+
# Usually these files are written by a python script from a template
36+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
37+
*.manifest
38+
*.spec
39+
40+
# Installer logs
41+
pip-log.txt
42+
pip-delete-this-directory.txt
43+
44+
# Unit test / coverage reports
45+
htmlcov/
46+
.tox/
47+
.nox/
48+
.coverage
49+
.coverage.*
50+
.cache
51+
nosetests.xml
52+
coverage.xml
53+
*.cover
54+
*.py,cover
55+
.hypothesis/
56+
.pytest_cache/
57+
pytestdebug.log
58+
59+
# Translations
60+
*.mo
61+
*.pot
62+
63+
# Django stuff:
64+
*.log
65+
local_settings.py
66+
db.sqlite3
67+
db.sqlite3-journal
68+
69+
# Flask stuff:
70+
instance/
71+
.webassets-cache
72+
73+
# Scrapy stuff:
74+
.scrapy
75+
76+
# Sphinx documentation
77+
docs/_build/
78+
doc/_build/
79+
80+
# PyBuilder
81+
target/
82+
83+
# Jupyter Notebook
84+
.ipynb_checkpoints
85+
86+
# IPython
87+
profile_default/
88+
ipython_config.py
89+
90+
# pyenv
91+
.python-version
92+
93+
# pipenv
94+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
95+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
96+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
97+
# install all needed dependencies.
98+
#Pipfile.lock
99+
100+
# poetry
101+
#poetry.lock
102+
103+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
104+
__pypackages__/
105+
106+
# Celery stuff
107+
celerybeat-schedule
108+
celerybeat.pid
109+
110+
# SageMath parsed files
111+
*.sage.py
112+
113+
# Environments
114+
# .env
115+
.env/
116+
.venv/
117+
env/
118+
venv/
119+
ENV/
120+
env.bak/
121+
venv.bak/
122+
pythonenv*
123+
124+
# Spyder project settings
125+
.spyderproject
126+
.spyproject
127+
128+
# Rope project settings
129+
.ropeproject
130+
131+
# mkdocs documentation
132+
/site
133+
134+
# mypy
135+
.mypy_cache/
136+
.dmypy.json
137+
dmypy.json
138+
139+
# Pyre type checker
140+
.pyre/
141+
142+
# pytype static type analyzer
143+
.pytype/
144+
145+
# operating system-related files
146+
# file properties cache/storage on macOS
147+
*.DS_Store
148+
# thumbnail cache on Windows
149+
Thumbs.db
150+
151+
# profiling data
152+
.prof
153+
154+
155+
# End of https://www.toptal.com/developers/gitignore/api/python
156+
n

doctest_sample.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def add(x, y):
2+
"""
3+
>>> add(3, 4)
4+
7
5+
>>> add(4, -1)
6+
3
7+
>>> add(0, 5)
8+
5
9+
"""
10+
return x + y
11+
12+
13+
def subtract(x, y):
14+
"""
15+
>>> subtract(3, 4)
16+
-1
17+
>>> subtract(4, -1)
18+
5
19+
>>> subtract(6, 5)
20+
1
21+
"""
22+
return x - y
23+
24+
25+
def multiply(x, y):
26+
"""
27+
>>> multiply(2, 4)
28+
8
29+
>>> multiply(4, -1)
30+
-4
31+
>>> multiply(2, 'reza')
32+
'rezareza'
33+
"""
34+
return x * y

main.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# This is a sample Python script.
2+
3+
# Press ⌃R to execute it or replace it with your code.
4+
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.
5+
6+
7+
def print_hi(name):
8+
# Use a breakpoint in the code line below to debug your script.
9+
print(f'Hi, {name}') # Press ⌘F8 to toggle the breakpoint.
10+
11+
12+
# Press the green button in the gutter to run the script.
13+
if __name__ == '__main__':
14+
print_hi('PyCharm')
15+
16+
# See PyCharm help at https://www.jetbrains.com/help/pycharm/

sample_func.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def add(x, y):
2+
return x + y
3+
4+
5+
def subtract(x, y):
6+
return x - y
7+
8+
9+
def multiply(x, y):
10+
return x * y
11+
12+
13+
def division(x, y):
14+
if y == 0:
15+
raise ZeroDivisionError("cant divide by zero. . .")
16+
return x / y

test_sample_func.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import unittest
2+
3+
from sample_func import add, subtract, multiply, division
4+
5+
6+
class OneTest(unittest.TestCase):
7+
def test_add(self):
8+
self.assertEqual(add(4, 9), 13)
9+
self.assertEqual(add(-3, 9), 6)
10+
11+
def test_subtract(self):
12+
self.assertEqual(subtract(4, 9), -5)
13+
self.assertEqual(subtract(-3, 9), -12)
14+
15+
def test_multiply(self):
16+
self.assertEqual(multiply(4, 9), 36)
17+
self.assertEqual(multiply(-3, 9), -27)
18+
19+
def test_division(self):
20+
self.assertEqual(round(division(4, 9)), 0)
21+
self.assertEqual(division(18, 9), 2)
22+
self.assertRaises(ZeroDivisionError, division, 4, 0)
23+
24+
25+
if __name__ == '__main__':
26+
unittest.main()

0 commit comments

Comments
 (0)