Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed testing framework (Pytest) #275

Merged
merged 19 commits into from
Jan 29, 2020
10 changes: 5 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ jobs:
command: |
. venv/bin/activate
if [ $(python -c "import sys; print(sys.version_info.minor)") -lt 8 ]; then
${PYTHON} -m coverage run -m unittest discover tests/ -v
${PYTHON} -m pip install --progress-bar off python-rapidjson==0.9.1 && coverage run -m unittest discover tests/ -v
${PYTHON} -m coverage run -m pytest tests/ -v
${PYTHON} -m pip install --progress-bar off python-rapidjson==0.9.1 && coverage run -m pytest tests/ -v
${PYTHON} -m coverage report -i
codecov
else
${PYTHON} -m unittest discover tests/ -v
${PYTHON} -m pip install --progress-bar off python-rapidjson==0.9.1 && ${PYTHON} -m unittest discover tests/ -v
${PYTHON} -m pytest tests/ -v
${PYTHON} -m pip install --progress-bar off python-rapidjson==0.9.1 && ${PYTHON} -m pytest tests/ -v
fi

- store_artifacts:
Expand Down Expand Up @@ -134,7 +134,7 @@ jobs:
- run:
name: run tests
command: |
python -m unittest discover tests/ -v
python -m pytest tests/ -v

check-metadata:
docker:
Expand Down
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Changed
+++++++

- Workspace directory is created when ``Project`` is initialized (#267, #271).
- Changed testing framework from ``unittest`` to ``pytest`` (#212, #275)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Changed testing framework from ``unittest`` to ``pytest`` (#212, #275)
- Changed testing framework from ``unittest`` to ``pytest`` (#212, #275).



Fixed
Expand Down
2 changes: 1 addition & 1 deletion doc/support.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ To run tests, execute:

.. code-block:: bash

(signac-dev) signac $ python -m unittest discover tests/
(signac-dev) signac $ python -m pytest tests/


Building documentation
Expand Down
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ h5py==2.10; implementation_name=='cpython'
tables==3.6.1; implementation_name=='cpython'
click>=7.0
ruamel.yaml>=0.15.89
pytest==5.3.4
pytest-subtests==0.3.0
180 changes: 89 additions & 91 deletions tests/test_buffered_mode.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Copyright (c) 2018 The Regents of the University of Michigan
# All rights reserved.
# This software is licensed under the BSD 3-Clause License.
import unittest
import os
import json
import logging
Expand All @@ -15,7 +14,8 @@
from signac.errors import BufferException
from signac.errors import BufferedFileError

from test_project import BaseProjectTest
from test_project import TestProjectBase
import pytest


PYPY = 'PyPy' in platform.python_implementation()
Expand All @@ -38,87 +38,87 @@
os.chmod(path, mode)


@unittest.skipIf(PYPY, "Buffered mode not supported for PyPy.")
class BufferedModeTest(BaseProjectTest):
@pytest.mark.skipif(PYPY, reason="Buffered mode not supported for PyPy.")
class TestBufferedMode(TestProjectBase):

def test_enter_exit_buffered_mode(self):
self.assertFalse(signac.is_buffered())
assert not signac.is_buffered()
with signac.buffered():
self.assertTrue(signac.is_buffered())
self.assertFalse(signac.is_buffered())
assert signac.is_buffered()
assert not signac.is_buffered()

self.assertFalse(signac.is_buffered())
assert not signac.is_buffered()
with signac.buffered():
self.assertTrue(signac.is_buffered())
assert signac.is_buffered()
with signac.buffered():
self.assertTrue(signac.is_buffered())
self.assertTrue(signac.is_buffered())
self.assertFalse(signac.is_buffered())
assert signac.is_buffered()
assert signac.is_buffered()
assert not signac.is_buffered()

def test_basic_and_nested(self):
job = self.project.open_job(dict(a=0))
job.init()
self.assertNotIn('a', job.doc)
assert 'a' not in job.doc
with signac.buffered():
self.assertNotIn('a', job.doc)
assert 'a' not in job.doc
job.doc.a = 0
self.assertEqual(job.doc.a, 0)
self.assertEqual(job.doc.a, 0)
assert job.doc.a == 0
assert job.doc.a == 0

with signac.buffered():
self.assertEqual(job.doc.a, 0)
assert job.doc.a == 0
job.doc.a = 1
self.assertEqual(job.doc.a, 1)
assert job.doc.a == 1
with signac.buffered():
self.assertEqual(job.doc.a, 1)
assert job.doc.a == 1
job.doc.a = 2
self.assertEqual(job.doc.a, 2)
self.assertEqual(job.doc.a, 2)
self.assertEqual(job.doc.a, 2)
assert job.doc.a == 2
assert job.doc.a == 2
assert job.doc.a == 2

def test_buffered_mode_force_write(self):
with signac.buffered(force_write=False):
with signac.buffered(force_write=False):
pass
self.assertFalse(signac.is_buffered())
assert not signac.is_buffered()

with signac.buffered(force_write=True):
with signac.buffered(force_write=True):
pass

with self.assertRaises(Error):
with pytest.raises(Error):
with signac.buffered():
with signac.buffered(force_write=True):
pass
self.assertFalse(signac.is_buffered())
assert not signac.is_buffered()

def test_buffered_mode_force_write_with_file_modification(self):
job = self.project.open_job(dict(a=0))
job.init()
job.doc.a = True
x = job.doc.a
self.assertEqual(job.doc.a, x)
with self.assertRaises(BufferedFileError):
assert job.doc.a == x
with pytest.raises(BufferedFileError):
with signac.buffered():
self.assertEqual(job.doc.a, x)
assert job.doc.a == x
job.doc.a = not x
self.assertEqual(job.doc.a, not x)
assert job.doc.a == (not x)
sleep(1.0)
with open(job.doc._filename, 'wb') as file:
file.write(json.dumps({'a': x}).encode())
self.assertFalse(signac.is_buffered())
self.assertEqual(job.doc.a, x)
assert not signac.is_buffered()
assert job.doc.a == x

with signac.buffered(force_write=True):
self.assertEqual(job.doc.a, x)
assert job.doc.a == x
job.doc.a = not x
self.assertEqual(job.doc.a, not x)
assert job.doc.a == (not x)
sleep(1.0)
with open(job.doc._filename, 'wb') as file:
file.write(json.dumps({'a': x}).encode())
self.assertEqual(job.doc.a, not x)
assert job.doc.a == (not x)

@unittest.skipIf(not ABLE_TO_PREVENT_WRITE, 'unable to trigger permission error')
@pytest.mark.skipif(not ABLE_TO_PREVENT_WRITE, reason='unable to trigger permission error')
def test_force_write_mode_with_permission_error(self):
job = self.project.open_job(dict(a=0))
job.init()
Expand All @@ -128,42 +128,42 @@ def test_force_write_mode_with_permission_error(self):
mode = os.stat(path).st_mode
logging.disable(logging.CRITICAL)
try:
self.assertEqual(job.doc.a, x)
with self.assertRaises(BufferedFileError):
assert job.doc.a == x
with pytest.raises(BufferedFileError):
with signac.buffered():
self.assertEqual(job.doc.a, x)
assert job.doc.a == x
job.doc.a = not x
self.assertEqual(job.doc.a, not x)
assert job.doc.a == (not x)
os.chmod(path, S_IREAD) # Trigger permissions error
finally:
logging.disable(logging.NOTSET)
os.chmod(path, mode)
self.assertEqual(job.doc.a, x)
assert job.doc.a == x

def test_buffered_mode_change_buffer_size(self):
self.assertFalse(signac.is_buffered())
assert not signac.is_buffered()
with signac.buffered(buffer_size=12):
self.assertTrue(signac.buffered())
self.assertEqual(signac.get_buffer_size(), 12)
assert signac.buffered()
assert signac.get_buffer_size() == 12

self.assertFalse(signac.is_buffered())
with self.assertRaises(TypeError):
assert not signac.is_buffered()
with pytest.raises(TypeError):
with signac.buffered(buffer_size=True):
pass

self.assertFalse(signac.is_buffered())
assert not signac.is_buffered()
with signac.buffered(buffer_size=12):
self.assertTrue(signac.buffered())
self.assertEqual(signac.get_buffer_size(), 12)
assert signac.buffered()
assert signac.get_buffer_size() == 12
with signac.buffered(buffer_size=12):
self.assertTrue(signac.buffered())
self.assertEqual(signac.get_buffer_size(), 12)
assert signac.buffered()
assert signac.get_buffer_size() == 12

self.assertFalse(signac.is_buffered())
with self.assertRaises(BufferException):
assert not signac.is_buffered()
with pytest.raises(BufferException):
with signac.buffered(buffer_size=12):
self.assertTrue(signac.buffered())
self.assertEqual(signac.get_buffer_size(), 12)
assert signac.buffered()
assert signac.get_buffer_size() == 12
with signac.buffered(buffer_size=14):
pass

Expand All @@ -175,77 +175,75 @@ def routine():
job.doc.a = True

for job in self.project:
self.assertTrue(job.sp.a > 0)
assert job.sp.a > 0
job.sp.a = - job.sp.a
self.assertTrue(job.sp.a < 0)
assert job.sp.a < 0
job2 = self.project.open_job(id=job.get_id())
self.assertTrue(job2.sp.a < 0)
assert job2.sp.a < 0
job.sp.a = - job.sp.a
self.assertTrue(job.sp.a > 0)
assert job.sp.a > 0
job2 = self.project.open_job(id=job.get_id())
self.assertTrue(job2.sp.a > 0)
assert job2.sp.a > 0

for job in self.project:
self.assertTrue(job.doc.a)
assert job.doc.a
job.doc.a = not job.doc.a
self.assertFalse(job.doc.a)
assert not job.doc.a
job.doc.a = not job.doc.a
self.assertTrue(job.doc.a)
assert job.doc.a

routine()
with signac.buffered():
self.assertTrue(signac.is_buffered())
assert signac.is_buffered()
routine()

for job in self.project:
x = job.doc.a
with signac.buffered():
self.assertEqual(job.doc.a, x)
assert job.doc.a == x
job.doc.a = not job.doc.a
self.assertEqual(job.doc.a, not x)
assert job.doc.a == (not x)
job2 = self.project.open_job(id=job.get_id())
self.assertEqual(job2.doc.a, not x)
self.assertEqual(job.doc.a, not x)
self.assertEqual(job2.doc.a, not x)
assert job2.doc.a == (not x)
assert job.doc.a == (not x)
assert job2.doc.a == (not x)

job.doc.a = x
with signac.buffered():
self.assertEqual(job.doc.a, x)
assert job.doc.a == x
job.doc.a = not x
self.assertEqual(job.doc.a, not x)
assert job.doc.a == (not x)
job2.doc.a = x
self.assertEqual(job.doc.a, x)
self.assertEqual(job2.doc.a, x)
self.assertEqual(job.doc.a, x)
self.assertEqual(job2.doc.a, x)
assert job.doc.a == x
assert job2.doc.a == x
assert job.doc.a == x
assert job2.doc.a == x

job.doc.a = x
with signac.buffered():
self.assertEqual(job.doc.a, x)
assert job.doc.a == x
job.doc.a = not x
self.assertEqual(job.doc.a, not x)
assert job.doc.a == (not x)
job2.doc.a = x
self.assertEqual(job.doc.a, x)
self.assertEqual(job2.doc.a, x)
assert job.doc.a == x
assert job2.doc.a == x
job.doc.a = not x
self.assertEqual(job.doc.a, not x)
self.assertEqual(job2.doc.a, not x)
self.assertEqual(job.doc.a, not x)
self.assertEqual(job2.doc.a, not x)
assert job.doc.a == (not x)
assert job2.doc.a == (not x)
assert job.doc.a == (not x)
assert job2.doc.a == (not x)

self.assertEqual(job.doc.a, not x)
with self.assertRaises(BufferedFileError) as cm:
assert job.doc.a == (not x)
with pytest.raises(BufferedFileError) as cm:
with signac.buffered():
self.assertEqual(job.doc.a, not x)
assert job.doc.a == (not x)
job.doc.a = x
self.assertEqual(job.doc.a, x)
assert job.doc.a == x
sleep(1.0)
with open(job.doc._filename, 'wb') as file:
file.write(json.dumps({'a': not x}).encode())
self.assertIn(job.doc._filename, cm.exception.files)
print(cm.value)
print(job.doc._filename)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove these prints.

assert job.doc._filename in cm.value.files
bdice marked this conversation as resolved.
Show resolved Hide resolved

break # only test for one job


if __name__ == '__main__':
unittest.main()
Loading