-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_project.py
More file actions
54 lines (51 loc) · 1.84 KB
/
test_project.py
File metadata and controls
54 lines (51 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import pytest
from unittest.mock import patch, mock_open
import csv
import random
from io import StringIO
from project import choose, save, randomize
@pytest.mark.parametrize("n, prompt_response, expected_output", [
(1, "1", "option1"),
(1, "2", "option2"),
(2, "1 and 2", ("option1", "option2")),
])
@patch("project.Prompt.ask")
def test_choose(mock_prompt, n, prompt_response, expected_output):
mock_prompt.return_value = prompt_response
result = choose(n, "option1", "option2", "option3")
assert result == expected_output
@patch("builtins.open", new_callable=mock_open)
@patch("csv.DictWriter")
def test_save(mock_dict_writer, mock_file):
mock_writer_instance = mock_dict_writer.return_value
inventory = ["item1", "item2"]
with patch("project.Inventory", inventory):
save("testfile", 10, 100, 50, "1", "1", 1)
mock_file.assert_called_once_with("testfile.csv", "w")
mock_writer_instance.writeheader.assert_called_once()
mock_writer_instance.writerow.assert_called_once_with({
"Level": 1,
"Attack": 10,
"Health": 100,
"Defense": 50,
"Defense Options": "1",
"Attack Options": "1",
"Inventory": inventory
})
@pytest.mark.parametrize("number, expected_choices", [
(1, ["Head", "Chest", "Legs"]),
(2, ["Head", "Chest", "Legs"]),
])
@patch("random.choice")
@patch("random.sample")
def test_randomize(mock_sample, mock_choice, number, expected_choices):
if number == 1:
mock_choice.return_value = "Head"
result = randomize(number)
assert result == "Head"
mock_choice.assert_called_once_with(expected_choices)
elif number == 2:
mock_sample.return_value = ("Head", "Chest")
result = randomize(number)
assert result == ("Head", "Chest")
mock_sample.assert_called_once_with(expected_choices, k=2)