forked from UWPCE-PythonCert/ProgrammingInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_random_pytest.py
More file actions
54 lines (40 loc) · 1.15 KB
/
Copy pathtest_random_pytest.py
File metadata and controls
54 lines (40 loc) · 1.15 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
#!/usr/bin/env python
"""
port of the random unit tests from the python docs to py.test
"""
import random
import pytest
example_seq = list(range(10))
def test_choice():
"""
A choice selected should be in the sequence
"""
element = random.choice(example_seq)
assert (element in example_seq)
def test_sample():
"""
All the items in a sample should be in the sequence
"""
for element in random.sample(example_seq, 5):
assert element in example_seq
def test_shuffle():
"""
Make sure a shuffled sequence does not lose any elements
"""
seq = list(range(10))
random.shuffle(seq)
seq.sort() # If you comment this out, it will fail, so you can see output
print("seq:", seq) # only see output if it fails
assert seq == list(range(10))
def test_shuffle_immutable():
"""
Trying to shuffle an immutable sequence raises an Exception
"""
with pytest.raises(TypeError):
random.shuffle((1, 2, 3))
def test_sample_too_large():
"""
Trying to sample more than exist should raise an error
"""
with pytest.raises(ValueError):
random.sample(example_seq, 20)