7 changed files with 184 additions and 9 deletions
@ -0,0 +1,28 @@ |
|||||||
|
name: lab-coverage |
||||||
|
|
||||||
|
on: [push] |
||||||
|
|
||||||
|
jobs: |
||||||
|
coverage: |
||||||
|
runs-on: ubuntu-latest |
||||||
|
steps: |
||||||
|
- uses: actions/checkout@v4 |
||||||
|
- uses: actions/setup-python@v4 |
||||||
|
with: |
||||||
|
python-version: '3.11.6' |
||||||
|
- name: Run image |
||||||
|
uses: abatilo/actions-poetry@v2 |
||||||
|
with: |
||||||
|
poetry-version: '1.7.1' |
||||||
|
- name: Install dependencies |
||||||
|
run: poetry install; poetry add pytest; poetry add pytest-mock; poetry add pytest-cov |
||||||
|
- name: Run pytest-cov |
||||||
|
run: poetry run pytest tests/ --cov=app --cov-branch --cov-report json:coverage.json |
||||||
|
- name: Check coverage threshold |
||||||
|
run: | |
||||||
|
coverage=$(jq -r '.totals.percent_covered' coverage.json) |
||||||
|
if [ $(echo "$coverage >= 85" | bc -l) -eq 1 ]; then |
||||||
|
echo "coverage $coverage% is ok" |
||||||
|
else |
||||||
|
exit 1 |
||||||
|
fi |
||||||
@ -0,0 +1,13 @@ |
|||||||
|
{"meta": |
||||||
|
{ |
||||||
|
"format": 2, "version": "7.4.3", "timestamp": "2024-03-03T21:30:46.186905", "branch_coverage": true, "show_contexts": false |
||||||
|
}, |
||||||
|
"files": |
||||||
|
{"app/__init__.py": |
||||||
|
{"executed_lines": [], "summary": |
||||||
|
{"covered_lines": 0, "num_statements": 0, "percent_covered": 100.0, "percent_covered_display": "100", "missing_lines": 0, "excluded_lines": 0, "num_branches": 0, "num_partial_branches": 0, "covered_branches": 0, "missing_branches": 0 |
||||||
|
}, |
||||||
|
"missing_lines": [], "excluded_lines": [], "executed_branches": [], "missing_branches": []}, "app/hangman.py": |
||||||
|
{"executed_lines": [5, 6, 9, 14, 98, 100, 102, 106, 120, 122, 124, 126, 127, 128, 129, 132, 133, 134, 135, 136, 137, 138, 141, 142, 143, 146, 147, 148, 151, 152, 153, 155, 156, 158, 160, 163, 166, 168, 169, 170, 173, 176, 177, 178, 180, 182, 183, 184, 188], |
||||||
|
"summary": { |
||||||
|
"covered_lines": 49, "num_statements": 55, "percent_covered": 90.41095890410959, "percent_covered_display": "90", "missing_lines": 6, "excluded_lines": 0, "num_branches": 18, "num_partial_branches": 1, "covered_branches": 17, "missing_branches": 1}, "missing_lines": [104, 108, 116, 117, 189, 190], "excluded_lines": [], "executed_branches": [[141, 142], [141, 146], [146, 147], [146, 151], [153, 155], [153, 160], [155, 156], [155, 158], [166, 168], [166, 173], [168, 132], [168, 169], [169, 168], [169, 170], [177, 124], [177, 178], [188, -5]], "missing_branches": [[188, 189]]}}, "totals": {"covered_lines": 49, "num_statements": 55, "percent_covered": 90.41095890410959, "percent_covered_display": "90", "missing_lines": 6, "excluded_lines": 0, "num_branches": 18, "num_partial_branches": 1, "covered_branches": 17, "missing_branches": 1}} |
||||||
@ -1,7 +1,57 @@ |
|||||||
# add your tests in the test_hangman |
from app.hangman import hangman |
||||||
# you may use parametrization if needed |
|
||||||
|
|
||||||
import pytest |
import pytest |
||||||
|
|
||||||
def test_hangman(): |
def choice(): |
||||||
assert True |
return 'python' |
||||||
|
|
||||||
|
|
||||||
|
def test_win(mocker, capsys): |
||||||
|
mocker.patch('app.hangman.get_random_word', choice) |
||||||
|
mocker.patch('builtins.input', side_effect=["p", "y", "t", "h", "o", "n", "n"]) |
||||||
|
hangman() |
||||||
|
captured = capsys.readouterr() |
||||||
|
assert "You win!" in captured.out |
||||||
|
|
||||||
|
|
||||||
|
def test_loose(mocker, capsys): |
||||||
|
mocker.patch('app.hangman.get_random_word', choice) |
||||||
|
mocker.patch('builtins.input', side_effect=["a", "b", "c", "d", "e", "f", "g", "n"]) |
||||||
|
hangman() |
||||||
|
captured = capsys.readouterr() |
||||||
|
assert "You lose!" in captured.out |
||||||
|
|
||||||
|
|
||||||
|
def test_play_again(mocker, capsys): |
||||||
|
mocker.patch('app.hangman.get_random_word', choice) |
||||||
|
#Здесь я сначала угадал python, потом нажал y для начала новой игры, снова угадал python, и не стал играть дальше |
||||||
|
mocker.patch('builtins.input', side_effect=["p", "y", "t", "h", "o", "n", "y", "p", "y", "t", "h", "o", "n", "n"]) |
||||||
|
hangman() |
||||||
|
captured = capsys.readouterr() |
||||||
|
assert "Goodbye!" in captured.out |
||||||
|
|
||||||
|
|
||||||
|
def test_invalid(mocker, capsys): |
||||||
|
mocker.patch('app.hangman.get_random_word', choice) |
||||||
|
#Invalid guess - число 1 |
||||||
|
mocker.patch('builtins.input', side_effect=["1", "p", "y", "t", "h", "o", "n", "n"]) |
||||||
|
hangman() |
||||||
|
captured = capsys.readouterr() |
||||||
|
assert "Invalid guess. Please enter a single letter." in captured.out |
||||||
|
|
||||||
|
|
||||||
|
def test_already_guessedd(mocker, capsys): |
||||||
|
mocker.patch('app.hangman.get_random_word', choice) |
||||||
|
#alreday guessed - y |
||||||
|
mocker.patch('builtins.input', side_effect=["p", "y", "y", "t", "h", "o", "n", "n"]) |
||||||
|
hangman() |
||||||
|
captured = capsys.readouterr() |
||||||
|
assert "You have already guessed that letter." in captured.out |
||||||
|
|
||||||
|
|
||||||
|
def test_keyboard_interupt(mocker, capsys): |
||||||
|
mocker.patch('app.hangman.get_random_word', choice) |
||||||
|
mocker.patch('builtins.input', side_effect=KeyboardInterrupt) |
||||||
|
with pytest.raises(SystemExit): |
||||||
|
hangman() |
||||||
|
captured = capsys.readouterr() |
||||||
|
assert "Goodbye!" in captured.out |
||||||
Loading…
Reference in new issue