summaryrefslogtreecommitdiff
path: root/testing/code
diff options
context:
space:
mode:
authorPrabhu Ramachandran2017-02-22 14:56:35 +0530
committerPrabhu Ramachandran2017-02-22 14:56:35 +0530
commitd5c20e445b193fad5cb4339f00e11212fdd39c0a (patch)
tree94425bf5693fd7797fe128678614b4de364b0df9 /testing/code
parentba725c5af9ac1c7064211589ff561fb79f3853b4 (diff)
downloadpython-workshops-d5c20e445b193fad5cb4339f00e11212fdd39c0a.tar.gz
python-workshops-d5c20e445b193fad5cb4339f00e11212fdd39c0a.tar.bz2
python-workshops-d5c20e445b193fad5cb4339f00e11212fdd39c0a.zip
Add basic slides for workshop on pytest.
Diffstat (limited to 'testing/code')
-rw-r--r--testing/code/gcd.py9
-rw-r--r--testing/code/test_class.py11
-rw-r--r--testing/code/test_fixtures.py51
-rw-r--r--testing/code/test_gcd.py21
-rw-r--r--testing/code/test_list_gitrepo.py46
5 files changed, 138 insertions, 0 deletions
diff --git a/testing/code/gcd.py b/testing/code/gcd.py
new file mode 100644
index 0000000..12e4935
--- /dev/null
+++ b/testing/code/gcd.py
@@ -0,0 +1,9 @@
+def gcd(a, b):
+ while b != 0:
+ a, b = b, a%b
+ return a
+
+def atoi(s):
+ return int(s)
+
+
diff --git a/testing/code/test_class.py b/testing/code/test_class.py
new file mode 100644
index 0000000..36f67bd
--- /dev/null
+++ b/testing/code/test_class.py
@@ -0,0 +1,11 @@
+
+class TestSomething(object):
+ def test_something_has_hello(self):
+ x = 'hello1'
+ y = 'hello world'
+ assert x in y
+
+ def test_something_else(self):
+ a = [1, 2, 3]
+ b = {1, 2, 3}
+ assert a == b
diff --git a/testing/code/test_fixtures.py b/testing/code/test_fixtures.py
new file mode 100644
index 0000000..4676509
--- /dev/null
+++ b/testing/code/test_fixtures.py
@@ -0,0 +1,51 @@
+import pytest
+
+
+@pytest.fixture
+def my_fixture():
+ print('my_fixture')
+
+
+def test_foo(my_fixture):
+ print('runing test')
+
+
+@pytest.fixture
+def fix1():
+ print('fix1')
+ yield
+ # Cleanup
+ print('Good bye: fix1')
+
+
+def test_fix1(fix1):
+ print('Running test_fix1')
+
+
+@pytest.fixture
+def fix2(my_fixture):
+ yield
+ # Cleanup
+ print('Good bye: fix2')
+
+
+def test_fix2(fix2):
+ print('Running test_fix2')
+
+@pytest.fixture
+def some_data(my_fixture):
+ print('some_data fixture')
+ yield {'a': 1, 'b': 2}
+ # Cleanup
+ print('Good bye: fix2')
+
+
+def test_some_data(some_data):
+ print(some_data)
+ print('Running test_fix2')
+
+
+
+@pytest.mark.usefixtures('fix1', 'fix2')
+def test_foo():
+ print('Running test_foo')
diff --git a/testing/code/test_gcd.py b/testing/code/test_gcd.py
new file mode 100644
index 0000000..d1ee7df
--- /dev/null
+++ b/testing/code/test_gcd.py
@@ -0,0 +1,21 @@
+from gcd import gcd, atoi
+
+def test_gcd():
+ assert gcd(48, 64) == 16
+ assert gcd(44, 19) == 1
+
+def test_atoi_works():
+ x = '1'
+ assert atoi(x) == 1
+
+import pytest
+
+def test_atoi_raises_error():
+ x = 'hello'
+ with pytest.raises(ValueError):
+ atoi(x)
+
+
+if __name__ == '__main__':
+ test_gcd()
+
diff --git a/testing/code/test_list_gitrepo.py b/testing/code/test_list_gitrepo.py
new file mode 100644
index 0000000..871e31f
--- /dev/null
+++ b/testing/code/test_list_gitrepo.py
@@ -0,0 +1,46 @@
+import tempfile
+import os
+import shutil
+import pytest
+
+
+def is_git_repo(path):
+ git_dir = os.path.join(path, '.git')
+ return os.path.isdir(git_dir)
+
+
+def find_all_git_repos(path):
+ pass
+
+
+@pytest.fixture
+def git_repo():
+ tmp_dir = tempfile.mkdtemp()
+ git_repo = os.path.join(tmp_dir, '.git')
+ if not os.path.exists(git_repo):
+ os.makedirs(git_repo)
+
+ yield tmp_dir
+
+ shutil.rmtree(tmp_dir)
+
+
+def test_is_git_repo_should_detect_git_repo(git_repo):
+ # Given
+ dir = git_repo
+
+ # When
+ result = is_git_repo(dir)
+
+ # Then
+ assert result == True
+
+
+def test_is_git_repo_should_not_detect_non_git_repo():
+ tmp_dir = tempfile.mkdtemp()
+ if not os.path.exists(tmp_dir):
+ os.makedirs(tmp_dir)
+ try:
+ assert is_git_repo(tmp_dir) == False
+ finally:
+ shutil.rmtree(tmp_dir)