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

Pytest Style: test_log.py #1467

Merged
merged 6 commits into from Jul 13, 2017

Conversation

Projects
None yet
4 participants
@utkbansal
Member

utkbansal commented Jul 10, 2017

Fixes #

Changes made in this Pull Request:

PR Checklist

  • Tests?
  • Docs?
  • CHANGELOG updated?
  • Issue raised/referenced?

@orbeckst orbeckst added the testing label Jul 11, 2017

@orbeckst

only 1 change, otherwise good

def _assert_in(output, string):
assert_(string in output,

This comment has been minimized.

@orbeckst

orbeckst Jul 11, 2017

Member

use plain pytest assert:

assert string in output, "Output '{0}' does not match required format '{1}'.".format(output.replace('\r', '\\r'), string.replace('\r', '\\r')))

utkbansal added some commits Jul 11, 2017

@utkbansal

This comment has been minimized.

Member

utkbansal commented Jul 11, 2017

@kain88-de Review please.

@jbarnoud

All occurrences of assert_raises should be replaced by the pytest equivalent. The deactivated assert_warn at the end of the file should be reactivated and moved to the pytest idiom.

The tests about the progress bar can probably be refactored as a single parametrized function.

from MDAnalysis.lib.log import _set_verbose
from numpy.testing import assert_, assert_equal, assert_raises

This comment has been minimized.

@jbarnoud

jbarnoud Jul 11, 2017

Contributor

assert_ and assert_raises should not be used.

def test_default_ProgressMeter(buffer, n=101, interval=10):
format = "Step {step:5d}/{numsteps} [{percentage:5.1f}%]"

This comment has been minimized.

@jbarnoud

jbarnoud Jul 11, 2017

Contributor

Nothing to do with pytest, but to avoid confusion could you rename this variable template? Same goes for all subsequent occurrences.

buffer.seek(0)
output = "".join(buffer.readlines())
_assert_in(output, (format + '\n').format(**{'step': 1, 'numsteps': n, 'percentage': 100./n}))
_assert_in(output, (format + '\n').format(**{'step': n, 'numsteps': n, 'percentage': 100.}))
def test__set_verbose():

This comment has been minimized.

@kain88-de

kain88-de Jul 11, 2017

Member

the below test screams for a mark.parametrize

Johnathan has some good points

utkbansal added some commits Jul 11, 2017

@utkbansal

This comment has been minimized.

Member

utkbansal commented Jul 11, 2017

Someone want's to verify why actually test_warnings__set_verbose fails?

@jbarnoud

I do not know why the warning tests fail. I check and the function does issue the warning... I'll try to have a deeper look tonight.

_assert_in(output, (template + '\n').format(**{'step': step, 'numsteps': n, 'percentage': percentage}))
class TestSetVerbose(object):

This comment has been minimized.

@jbarnoud

jbarnoud Jul 12, 2017

Contributor

Why the class?

This comment has been minimized.

@utkbansal

utkbansal Jul 12, 2017

Member

Because there are two methods that basically are testing the same log think. I thought it makes sense to group them. No?

This comment has been minimized.

@jbarnoud

jbarnoud Jul 12, 2017

Contributor

Makes sense indeed.

assert string in output, "Output '{0}' does not match required format '{1}'.".format(output.replace('\r', '\\r'), string.replace('\r', '\\r'))
def test_default_ProgressMeter(buffer, n=101, interval=10):

This comment has been minimized.

@jbarnoud

jbarnoud Jul 12, 2017

Contributor

The test_*_ProgressMeter functions are basicaly all doing the same thing. They should be parametrized.

@pytest.fixture()
def buffer():

This comment has been minimized.

@jbarnoud

jbarnoud Jul 12, 2017

Contributor

Good that you renamed buf to buffer. It was a terrible name, my bad.

@jbarnoud

This comment has been minimized.

Contributor

jbarnoud commented Jul 12, 2017

Someone want's to verify why actually test_warnings__set_verbose fails?

This seems to be the answer: https://docs.pytest.org/en/latest/warnings.html#ensuring-function-triggers

@utkbansal

This comment has been minimized.

Member

utkbansal commented Jul 12, 2017

@jbarnoud @kain88-de @richardjgowers

  • test_not_dynamic_ProgressMeter has a different _assert_in statement - it doesn't have \r so that will remain seperate.

  • test_default_ProgressMeter has

pm = MDAnalysis.lib.log.ProgressMeter(n, interval=interval)
        for frame in range(n):
            pm.echo(frame)

remaining two have (test_custom_ProgressMeter & test_legacy_ProgressMeter)

pm = MDAnalysis.lib.log.ProgressMeter(n, interval=interval,
                                              format=template, offset=1)
        for frame in range(n):
            rmsd = 0.02 * frame * (n+1)/float(n)  # n+1/n correction for 0-based frame vs 1-based counting
            pm.echo(frame, rmsd=rmsd)

so there is a change in rmsd hence test_default_ProgressMeter can't be merged either.

In the end I think I can only merge test_custom_ProgressMeter & test_legacy_ProgressMeter into one and parametrize them. Am I correct?

@jbarnoud

This comment has been minimized.

Contributor

jbarnoud commented Jul 12, 2017

I still think there is a way to avoid the redundancy, but it may induce some complexity we do not want in the tests. Do not loose time on this.

@utkbansal

This comment has been minimized.

Member

utkbansal commented Jul 12, 2017

So what do we do? Merge only the two cases? Or merge the PR as is?

@jbarnoud

This comment has been minimized.

Contributor

jbarnoud commented Jul 12, 2017

If you fix the warning tests, I'll merged as it.

@utkbansal

This comment has been minimized.

Member

utkbansal commented Jul 12, 2017

@jbarnoud Added these lines to the test

warnings.simplefilter('always')
warnings.warn("deprecated", DeprecationWarning)

Now pytest utils/test_log.py::TestSetVerbose::test_warnings__set_verbose passes but pytest utils/test_log.py::TestSetVerbose fails.

@jbarnoud

This comment has been minimized.

Contributor

jbarnoud commented Jul 12, 2017

Why not using pytest.deprecated_call?

@utkbansal

This comment has been minimized.

Member

utkbansal commented Jul 13, 2017

@jbarnoud This is done I think.

@jbarnoud jbarnoud merged commit ae6fca7 into MDAnalysis:develop Jul 13, 2017

3 checks passed

QuantifiedCode 2 minor issues introduced. 1 issue(s) fixed.
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage increased (+0.7%) to 90.337%
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment