Skip to content

Commit 19d56f4

Browse files
committed
Tests 4 W06
1 parent 213e67a commit 19d56f4

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

.github/workflows/python-app-week-05.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This workflow will install Python dependencies, run tests and lint with a single version of Python
22
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
33

4-
name: Week04 Homework
4+
name: Week05 Homework
55

66
on:
77
push:
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# This workflow will install Python dependencies, run tests and lint with a single version of Python
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
3+
4+
name: Week06 Homework
5+
6+
on:
7+
push:
8+
branches: [ "master" ]
9+
paths: ['Week06/**']
10+
pull_request:
11+
branches: [ "master" ]
12+
paths: ['Week06/**']
13+
14+
permissions:
15+
contents: read
16+
17+
jobs:
18+
build:
19+
20+
runs-on: ubuntu-latest
21+
22+
steps:
23+
- uses: actions/checkout@v3
24+
- name: Set up Python 3.12
25+
uses: actions/setup-python@v3
26+
with:
27+
python-version: "3.12"
28+
- name: Install dependencies
29+
run: |
30+
python -m pip install --upgrade pip
31+
pip install flake8 pytest
32+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
33+
- name: Lint with flake8
34+
run: |
35+
# stop the build if there are Python syntax errors or undefined names
36+
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
37+
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
38+
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
39+
- name: Test with pytest
40+
run: |
41+
pytest -q --tb=no 'Week06/test_timer.py'

Week06/test_timer.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
import pytest
3+
import time
4+
5+
6+
files = [f for f in os.listdir(os.path.dirname(__file__)) if f.startswith("timer")]
7+
for f in files:
8+
exec("import " + f[:-3] + " as " + f[:-3])
9+
10+
11+
def test_names():
12+
for f in files:
13+
assert "Timer" in dir(eval(f[:-3])), "Timer is not defined in " + f[:-3]
14+
15+
16+
def test_context_manager():
17+
for f in files:
18+
assert hasattr(eval(f[:-3]).Timer, "__enter__"), (
19+
"Timer is not a context manager in " + f[:-3]
20+
)
21+
assert hasattr(eval(f[:-3]).Timer, "__exit__"), (
22+
"Timer is not a context manager in " + f[:-3]
23+
)
24+
25+
26+
def test_time_taken():
27+
for f in files:
28+
with eval(f[:-3]).Timer() as timer:
29+
time.sleep(2)
30+
assert 2.1 >= timer.end_time - timer.start_time >= 2, (
31+
"Timer is not working in " + f[:-3]
32+
)
33+
34+
35+
if __name__ == "__main__":
36+
pytest.main(["-v", __file__])

0 commit comments

Comments
 (0)