initial creation
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
def ham(n):
|
||||
return n * 10
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/env python
|
||||
from pytest import fixture
|
||||
|
||||
@fixture
|
||||
def common_fixture(): # <1>
|
||||
return "DATA"
|
||||
|
||||
|
||||
def pytest_runtest_setup(item): # <2>
|
||||
print("Hello from setup,", item)
|
||||
@@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env python
|
||||
import pytest
|
||||
|
||||
def test_one(): # <1>
|
||||
print("WHOOPEE")
|
||||
assert(1)
|
||||
|
||||
def test_two(common_fixture): # <2>
|
||||
assert(common_fixture == "DATA")
|
||||
|
||||
if __name__ == '__main__':
|
||||
pytest.main([__file__, "-s"]) # <3>
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
import pytest
|
||||
|
||||
def test_two_plus_two_equals_four():
|
||||
assert 2 + 2 == 4
|
||||
@@ -0,0 +1,34 @@
|
||||
COUNTER_KEY = 'test_cache/counter'
|
||||
|
||||
def test_cache(cache): # <1>
|
||||
value = cache.get(COUNTER_KEY, 0)
|
||||
print("Counter before:", value)
|
||||
cache.set(COUNTER_KEY, value + 1) # <2>
|
||||
value = cache.get(COUNTER_KEY, 0) # <2>
|
||||
print("Counter after:", value)
|
||||
assert True # <3>
|
||||
|
||||
def hello():
|
||||
print("Hello, pytesting world")
|
||||
|
||||
def test_capsys(capsys):
|
||||
hello() # <4>
|
||||
out, err = capsys.readouterr() # <5>
|
||||
print("STDOUT:", out)
|
||||
|
||||
def bhello():
|
||||
print(b"Hello, binary pytesting world\n")
|
||||
|
||||
def test_capsysbinary(capsys):
|
||||
bhello() # <6>
|
||||
out, err = capsys.readouterr() # <7>
|
||||
print("BINARY STDOUT:", out)
|
||||
|
||||
def test_temp_dir1(tmpdir):
|
||||
print("TEMP DIR:", str(tmpdir)) # <8>
|
||||
|
||||
def test_temp_dir2(tmpdir):
|
||||
print("TEMP DIR:", str(tmpdir))
|
||||
|
||||
def test_temp_dir3(tmpdir):
|
||||
print("TEMP DIR:", str(tmpdir))
|
||||
@@ -0,0 +1,51 @@
|
||||
#!/usr/bin/env python
|
||||
import pytest
|
||||
|
||||
class Spam():
|
||||
def get_five(self):
|
||||
return 5
|
||||
|
||||
@pytest.fixture
|
||||
def spam():
|
||||
return Spam()
|
||||
|
||||
@pytest.fixture
|
||||
def colors():
|
||||
return ['pink', 'orange', 'purple', 'green']
|
||||
|
||||
@pytest.fixture
|
||||
def before_after():
|
||||
print("\n******* BEFORE! *******")
|
||||
yield 100
|
||||
print("\n"
|
||||
"******* AFTER! *******")
|
||||
|
||||
def test_get_five_returns_five(spam):
|
||||
assert spam.get_five() == 5
|
||||
|
||||
def test_spam_has_get_five_method(spam):
|
||||
assert hasattr(spam, "get_five")
|
||||
|
||||
def test_some_color_is_pink(colors):
|
||||
assert any(color == 'pink' for color in colors)
|
||||
|
||||
@pytest.mark.beforeafter
|
||||
def test_beforeafter(before_after, colors):
|
||||
assert before_after == 100
|
||||
assert 'green' in colors
|
||||
|
||||
def test_temp_dir1(tmpdir):
|
||||
print("TEMP DIR:", str(tmpdir))
|
||||
assert 1
|
||||
|
||||
def test_temp_dir2(tmpdir):
|
||||
print("TEMP DIR:", str(tmpdir))
|
||||
assert 1
|
||||
|
||||
def test_temp_dir3(tmpdir):
|
||||
print("TEMP DIR:", str(tmpdir))
|
||||
assert 1
|
||||
|
||||
def test_temp_dir4(tmpdir):
|
||||
print("TEMP DIR:", str(tmpdir))
|
||||
assert 1
|
||||
@@ -0,0 +1,27 @@
|
||||
#!/usr/bin/env python
|
||||
import pytest
|
||||
|
||||
|
||||
class Dog:
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def dog(request):
|
||||
return Dog(request.param)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'dog',
|
||||
('Lassie', 'Rin-tin-tin', 'Asta', 'Toto'),
|
||||
indirect=True
|
||||
)
|
||||
def test_dog_name_is_string(dog):
|
||||
print(dog.name)
|
||||
assert isinstance(dog.name, str)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, '-s'])
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env python
|
||||
import pytest
|
||||
|
||||
@pytest.mark.alpha # <1>
|
||||
def test_one():
|
||||
assert 1
|
||||
|
||||
@pytest.mark.alpha # <1>
|
||||
def test_two():
|
||||
assert 1
|
||||
|
||||
@pytest.mark.beta # <2>
|
||||
def test_three():
|
||||
assert 1
|
||||
|
||||
if __name__ == '__main__':
|
||||
pytest.main([__file__, '-m alpha']) # <3>
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env python
|
||||
import pytest
|
||||
from unittest.mock import Mock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def small_list(): # <1>
|
||||
return [1, 2, 3]
|
||||
|
||||
|
||||
def test_m1_returns_correct_list(small_list):
|
||||
m1 = Mock(return_value=small_list) # <2>
|
||||
mock_result = m1('a', 'b') # <3>
|
||||
assert mock_result == small_list # <4>
|
||||
|
||||
|
||||
m2 = Mock() # <5>
|
||||
|
||||
m2.spam('a', 'b') # <6>
|
||||
m2.ham('wombat') # <6>
|
||||
m2.eggs(1, 2, 3) # <6>
|
||||
|
||||
print("mock calls:", m2.mock_calls) # <7>
|
||||
|
||||
m2.spam.assert_called_with('a', 'b') # <8>
|
||||
@@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env python
|
||||
import pytest # <1>
|
||||
import re # <2>
|
||||
|
||||
class SpamSearch(): # <3>
|
||||
def __init__(self, search_string, target_string):
|
||||
self.search_string = search_string
|
||||
self.target_string = target_string
|
||||
|
||||
|
||||
def findit(self): # <4>
|
||||
return re.search(self.search_string, self.target_string)
|
||||
|
||||
def test_spam_search_calls_re_search(mocker): # <5>
|
||||
mocker.patch('re.search') # <6>
|
||||
s = SpamSearch('bug', 'lightning bug') # <7>
|
||||
_ = s.findit() # <8>
|
||||
re.search.assert_called_once_with('bug', 'lightning bug') # <9>
|
||||
|
||||
if __name__ == '__main__':
|
||||
pytest.main([__file__, '-s']) # <10>
|
||||
@@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env python
|
||||
import pytest # <1>
|
||||
import hamlib # <2>
|
||||
|
||||
class Barbecue(): # <3>
|
||||
|
||||
def doit(self): # <4>
|
||||
return hamlib.ham(8)
|
||||
|
||||
def test_barbecue_doit_calls_ham(mocker): # <5>
|
||||
mocker.patch('hamlib.ham') # <6>
|
||||
b = Barbecue()
|
||||
result = b.doit()
|
||||
hamlib.ham.assert_called_once_with(8) # <9>
|
||||
|
||||
if __name__ == '__main__':
|
||||
pytest.main([__file__, '-s']) # <10>
|
||||
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
import pytest
|
||||
from unittest.mock import Mock
|
||||
|
||||
RETURN_VALUE = 99
|
||||
|
||||
ham = Mock(return_value=RETURN_VALUE) # <1>
|
||||
|
||||
|
||||
class Spam(): # <2>
|
||||
def __init__(self):
|
||||
self._value = ham() # <3>
|
||||
|
||||
@property
|
||||
def value(self): # <4>
|
||||
return self._value
|
||||
|
||||
|
||||
# dependency to be mocked -- not used in test
|
||||
# def ham():
|
||||
# return 42
|
||||
|
||||
def test_whatever(): # <5>
|
||||
spam = Spam() # <6>
|
||||
assert RETURN_VALUE == spam.value # <7>
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pytest.main([__file__])
|
||||
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
import pytest
|
||||
from unittest.mock import Mock
|
||||
|
||||
ham = Mock() # <1>
|
||||
|
||||
|
||||
# system under test
|
||||
class Spam(): # <2>
|
||||
def __init__(self, param):
|
||||
self._value = ham(param) # <3>
|
||||
|
||||
@property
|
||||
def value(self): # <4>
|
||||
return self._value
|
||||
|
||||
# dependency to be mocked -- not used in test
|
||||
# def ham(n):
|
||||
# pass
|
||||
|
||||
def test_spam_calls_ham(): # <5>
|
||||
_ = Spam(42) # <6>
|
||||
ham.assert_called_once_with(42) # <7>
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pytest.main([__file__])
|
||||
@@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env python
|
||||
import pytest
|
||||
|
||||
|
||||
def triple(x): # <1>
|
||||
return x * 3
|
||||
|
||||
test_data = [(5, 15), ('a', 'aaa'), ([True], [True, True, True])] # <2>
|
||||
|
||||
@pytest.mark.parametrize("input,result", test_data) # <3>
|
||||
def test_triple(input, result): # <4>
|
||||
print("input {} result {}:".format(input, result)) # <4>
|
||||
assert triple(input) == result # <5>
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, '-s'])
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
def test_two_plus_two_equals_four(): # <1>
|
||||
assert 2 + 2 == 4 # <2>
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env python
|
||||
from collections import namedtuple
|
||||
import pytest
|
||||
|
||||
Person = namedtuple('Person', 'first_name last_name') # <1>
|
||||
|
||||
FIRST_NAME = "Guido"
|
||||
LAST_NAME = "Von Rossum"
|
||||
|
||||
@pytest.fixture # <2>
|
||||
def person():
|
||||
"""
|
||||
Return a 'Person' named tuple with fields 'first_name' and 'last_name'
|
||||
"""
|
||||
return Person(FIRST_NAME, LAST_NAME) # <3>
|
||||
|
||||
|
||||
def test_first_name(person): # <4>
|
||||
assert person.first_name == FIRST_NAME
|
||||
|
||||
def test_last_name(person): # <4>
|
||||
assert person.last_name == LAST_NAME
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env python
|
||||
import sys
|
||||
import pytest
|
||||
|
||||
def test_one(): # <1>
|
||||
assert 1
|
||||
|
||||
@pytest.mark.skip(reason="can not currently test") # <2>
|
||||
def test_two():
|
||||
assert 1
|
||||
|
||||
@pytest.mark.skipif(sys.platform != 'win32', reason="only implemented on Windows") # <3>
|
||||
def test_three():
|
||||
assert 1
|
||||
|
||||
@pytest.mark.xfail # <4>
|
||||
def test_four():
|
||||
assert 1
|
||||
|
||||
@pytest.mark.xfail # <4>
|
||||
def test_five():
|
||||
assert 0
|
||||
|
||||
if __name__ == '__main__':
|
||||
pytest.main([__file__, '-v'])
|
||||
@@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env python
|
||||
import pytest
|
||||
import math
|
||||
|
||||
FILE_NAME = 'IDONOTEXIST.txt'
|
||||
|
||||
def test_missing_filename():
|
||||
with pytest.raises(FileNotFoundError): # <1>
|
||||
open(FILE_NAME) # <2>
|
||||
|
||||
def test_list():
|
||||
print()
|
||||
assert (.1 + .2) == pytest.approx(.3) # <3>
|
||||
|
||||
def test_approximate_pi():
|
||||
assert 22 / 7 == pytest.approx(math.pi, .001) # <4>
|
||||
|
||||
Reference in New Issue
Block a user