Conversation
8b95a78 to
4a81a97
Compare
| assert frame.lmbda == 0.5 | ||
|
|
||
|
|
||
| class XDRFormatBaseTest(object): |
There was a problem hiding this comment.
You could have kept the class and parametrised it - do you prefer the standalone functions instead?
There was a problem hiding this comment.
I prefer the stand alone funtions. A neat thing about them is that I see all variables effecting a test at once. No need to scroll and it is immediately clear which files are going to be tested.
| assert_equal(f_out.n_atoms, 0) | ||
| for frame in f_in: | ||
| @pytest.mark.parametrize('dtype', (np.int32, np.int64, np.float32, np.float64, int, float)) | ||
| def test_write_trr_dtype(tmpdir, dtype): |
There was a problem hiding this comment.
Something I have in another project is a in_tmpdir fixture, which is something like:
def in_tmpdir(tmpdir):
with tmpdir.as_cwd():
yieldBut it then saves you another level of indentation as the fixture is dealing with teardown. Worth putting in?
There was a problem hiding this comment.
then we have def test_a(in_tmpdir): which runs even more magically in a temporary directory and we don't use the in_tmpdir variable. Not using a variable isn't so nice since I would like to enable pylint to warn about unused variables soon.
So good idea. But maybe to much magic
4a81a97 to
13e50d3
Compare
|
So does this conflict with anything that @utkbansal is currently working on? |
|
It shouldn't he already ported formats to pytest a while ago. I just wanted to add new useful tests that @tylerjereddy wrote for libdcd. |
|
I admit I might have been a bit over enthusiastic implementing pytest idoms in the tests. |
|
|
||
|
|
||
| def test_xtc(): | ||
| f = XTCFile(XTC_single_frame) |
There was a problem hiding this comment.
won't this open a file handle that isn't otherwise cleaned up?
There was a problem hiding this comment.
I fixed that. I didn't see any before because the function should clean up nicely and call dealloc. But lets be on the safe side
|
|
||
| @pytest.mark.parametrize('dtype', (np.int32, np.int64, np.float32, np.float64, int, float)) | ||
| def test_write_xtc_dtype(tmpdir, dtype): | ||
| with tmpdir.as_cwd(): |
There was a problem hiding this comment.
So maybe having a unused fixture isn't any better, but I prefer the decorator approach to tmpdir. We're only using the inside of the context manager, so it's cleaner to not have the indentation.
|
Yeah, I think PRs like this will probably set up which idioms etc we'll use globally. |
13e50d3 to
b10e5e5
Compare
I opened an issue #1444 to talk about this a bit. |
| # xtc only saves with 3 decimal places precision | ||
| assert_array_almost_equal(xyz.flat, np.arange(30), decimal=3) | ||
| assert_array_almost_equal(box, np.eye(3) * 20, decimal=3) | ||
| assert step == 0 |
There was a problem hiding this comment.
I don't like having multiple asserts in one test function, it makes it harder to debug. Eg, if xyz.flat stopped working, I'd also want to know if box was working too, to help me try and locate the problem. This test wouldn't give me the breakdown of what is broken.
I know you're not a fan of classes, but ..
class TestXTC(object):
@staticmethod
@pytest.fixture()
def XTC():
with XTCFile(XTC_single_frame) as f:
yield f
def test_n_atoms(self, XTC):
assert XTC.n_atoms == 10
def test_xyc(self, XTC):
etcThis is a lot nicer imo
There was a problem hiding this comment.
I like the XTC fixture. I think I will add that one.
package/CHANGELOG
Outdated
|
|
||
| Enhancements | ||
| * libmdaxdr classes now accept more argument types for a write (PR #1442) | ||
| * libmdaxdr classes now raise EOFError when accessing another frame after |
There was a problem hiding this comment.
I think this counts as a "Change", not an enhancement. State what it used to do, IOError,
|
|
||
| from numpy.testing import assert_almost_equal | ||
| from numpy.testing import assert_array_almost_equal | ||
| from numpy.testing import assert_array_equal |
There was a problem hiding this comment.
Just out of curiosity: why not
from numpy.testing import assert_array_almost_equal, assert_array_equal?
| box_compare = np.eye(3) * 20 | ||
| for i, frame in enumerate(f): | ||
| xyz, box, step, time, prec = frame | ||
| assert_array_almost_equal(xyz, ones * i, decimal=3) |
There was a problem hiding this comment.
Split into two tests? See @richardjgowers 's comment above.
|
|
||
| @pytest.mark.parametrize("xdrfile, fname", ((XTCFile, XTC_multi_frame), | ||
| (TRRFile, TRR_multi_frame))) | ||
| def test_over_seek(xdrfile, fname): |
There was a problem hiding this comment.
I'm unable in this test to use the xtc and trr fixtures above with parametrize.
| @pytest.mark.parametrize( | ||
| 'dtype', (np.int32, np.int64, np.float32, np.float64, int, float)) | ||
| def test_write_xtc_dtype(tmpdir, dtype, xtc): | ||
| outname = str(tmpdir.join('foo.xtc')) |
There was a problem hiding this comment.
@richardjgowers This is another option to use tmpdir. It avoids the extra indentation.
|
@utkbansal input from you would be appreciated here and in #1444 as well since you will also spend a lot of time implementing the guides we are setting now. |
caa971f to
db5172a
Compare
| assert f._bytes_tell() == big_offset | ||
|
|
||
|
|
||
| def test_read_multi_frame_xtc(xtc): |
There was a problem hiding this comment.
I know I still need to untangle these tests
|
|
||
|
|
||
| @pytest.mark.parametrize('array_like', (np.array, list)) | ||
| def test_write_xtc_array_like(tmpdir, array_like, xtc): |
There was a problem hiding this comment.
I know these tests should be untangled as well. I also think I know how to do that best giving the granularity in the tests that @richardjgowers likes.
|
|
||
| @pytest.mark.parametrize('fname, xdr', ((XTC_multi_frame, XTCFile), | ||
| (TRR_multi_frame, TRRFile))) | ||
| class TestCommonAPI: |
There was a problem hiding this comment.
@richardjgowers is that what you have in common with using classes to parametrize common tests?
There was a problem hiding this comment.
I do admit that this removes a lot of boiler plate for these tests
|
|
||
| def test_seek_overflow(self, reader, offsets): | ||
| with pytest.raises(OverflowError): | ||
| reader._bytes_seek(2**65) |
There was a problem hiding this comment.
there is a bad side effect though. We need to always use all parametrized values to a function even if they are not used.
| def test_box_xtc(xtc): | ||
| box = np.eye(3) * 20 | ||
| for frame in xtc: | ||
| assert_array_almost_equal(frame.box, box, decimal=3) |
There was a problem hiding this comment.
So here I decided to go with special functions since the API of XTC and TRR are different.
| for frame in xtc: | ||
| f.write(*frame) | ||
| with XTCFile(fname) as f: | ||
| yield f |
There was a problem hiding this comment.
these fixtures with yield are really nice. They do remove a hell of a lot of boilerplate code
dcee674 to
ee5f872
Compare
|
@kain88-de I was playing around with pytest... @pytest.fixture(params=(
(XTC_multi_frame, XTCFile),
(TRR_multi_frame, TRRFile),
))
def reader(request):
fname, cls = request.param
with cls(fname) as f:
yield f
class TestCommonAPI(object):
def test_natoms(self, reader):
assert reader.n_atoms == 10
def test_over_seek(self, reader):
with pytest.raises(EOFError):
reader.seek(100)So you can define a |
|
@richardjgowers that is pretty cool. But then we really don't need the class anymore. |
|
This pattern would yield parametrize over fixtures and single functions instead of parametrize over classes. |
| XTC_sub_sol = resource_filename(__name__, 'data/cobrotoxin.xtc') | ||
| PDB_sub_sol = resource_filename(__name__, 'data/cobrotoxin.pdb') | ||
| PDB_xlserial = resource_filename(__name__, 'data/xl_serial.pdb') | ||
| XTC_single_frame = resource_filename( |
There was a problem hiding this comment.
Did we remove the XTC and TRR single frame files?
There was a problem hiding this comment.
yes I noticed they weren't needed in the first place. They test nothing that isn't tested in the other files
|
@richardjgowers anything left to be done on this? |
ee5f872 to
909097b
Compare
Changes made in this Pull Request:
PR Checklist
- [x] Issue raised/referenced?