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

Rewrite tests with pytest fixtures #2948

Merged
36 commits merged into from
Mar 12, 2018
Merged

Rewrite tests with pytest fixtures #2948

36 commits merged into from
Mar 12, 2018

Conversation

heartsucker
Copy link
Contributor

@heartsucker heartsucker commented Jan 28, 2018

Status

Ready for review

Description of Changes

Work towards #2877

Added pytest fixtures to centralize app creation logic for the source app

Testing

make test

Checklist

If you made changes to the app code:

  • Unit and functional tests pass on the development VM

@kushaldas
Copy link
Contributor

I think somehow the cache is causing trouble in CircleCI. make test passes properly in local system.

@heartsucker
Copy link
Contributor Author

@kushaldas That's what I thought too. I guess I'll open a ticket for that.

@heartsucker
Copy link
Contributor Author

For now, it seems like this PR is blocked by #3033. Once that is merged, the strange test failure logic related to this line:

c = imp.load_module('c', *imp.find_module('config'))

should go away. But to be fair, I'm not positive that's the line that's causing problems, but it's definitely the most likely one.

@codecov-io
Copy link

codecov-io commented Feb 28, 2018

Codecov Report

Merging #2948 into develop will increase coverage by 4.33%.
The diff coverage is n/a.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #2948      +/-   ##
===========================================
+ Coverage    84.31%   88.64%   +4.33%     
===========================================
  Files           34       32       -2     
  Lines         2027     1849     -178     
  Branches       221      212       -9     
===========================================
- Hits          1709     1639      -70     
+ Misses         262      162     -100     
+ Partials        56       48       -8
Impacted Files Coverage Δ
journalist_app/account.py 92.85% <0%> (-0.33%) ⬇️
journalist_app/utils.py 86.23% <0%> (-0.22%) ⬇️
source_app/main.py 97.77% <0%> (-0.04%) ⬇️
create-dev-data.py
i18n_tool.py
sdconfig.py
create-demo-user.py 0% <0%> (ø)
journalist_app/admin.py 89.03% <0%> (+0.59%) ⬆️
source_app/utils.py 87.27% <0%> (+1.55%) ⬆️
crypto_util.py 97.91% <0%> (+1.99%) ⬆️
... and 4 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 094f4fa...0b25f12. Read the comment docs.

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Excellent 💯, really happy this is done. I won't lie: it is not the most entertaining PR to review. But you made it very easy for the reviewer by clearly separating each commit, thanks a lot for this.

I have just one quesiton and two nits, other than this I think it is good to merge.

assert "BEGIN PGP PUBLIC KEY BLOCK" in text

def test_login_and_logout(self, source_app):
with source_app.test_client() as app:
Copy link

Choose a reason for hiding this comment

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

Nit: Would it be a problem to keep multiple with source_app.test_client() as app: to separate the blocks?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why is the grounds for that nit? I think it might do some silly things to sessions and whatnot (but could be wrong).

Copy link
Contributor

Choose a reason for hiding this comment

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

Since the code in each block is a separate test case, having them use a different test client makes sense (really they should be in different tests entirely - but in the spirit of not expanding the scope of this PR beyond strictly rewriting the tests to use pytest fixtures, @dachary's suggestion seems reasonable)

login_test(app, codename_)

@staticmethod
def _dummy_submission(app):
Copy link

Choose a reason for hiding this comment

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

Nit: for clarity removal of the old _dummy_submission is in commit pytest fixtures: test_csrf_error_page but should be in commit pytest fixtures: test_initial_submission_notification where the static version is introduced.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't move all the tests at once, so I leave it in the old class because there are tests that required it,

session.pop('csrf_token', None)
assert not session, session
text = resp.data.decode('utf-8')
assert 'Your session timed out due to inactivity' in text
Copy link

Choose a reason for hiding this comment

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

Why is it different from You have been logged out due to inactivity ? It indicates the test does not behave in the same way and this is either cause for concern or a detailed explanation in the commit message.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That string never appears in the source app, and also that test was prefixed with _test so it was never actually run before. We have higher coverage now that it is actually run.

Copy link
Contributor

@redshiftzero redshiftzero left a comment

Choose a reason for hiding this comment

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

looks great, thanks for the organized git history here :) one nit inline and one optional suggestion to look at - otherwise I'm happy with this being merged

follow_redirects=True)
assert resp.status_code == 200
text = resp.data.decode('utf-8')
assert "Submit Materials" in text, text
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: in text, text -> in text

Copy link

Choose a reason for hiding this comment

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

If I'm not mistaken the idea here is to display text in case the assert fails but ... pytest instruments the assert and displays the actual values being compared when it fails. So +1 on the Nit ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Honestly, we should probably do that with all cases since seeing ... in the error output is not very helpful, but that one probably got left in by mistake.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed


overly_long_codename = 'a' * (Source.MAX_CODENAME_LEN + 1)


class TestSourceApp(TestCase):
class TestSourceApp:
Copy link
Contributor

Choose a reason for hiding this comment

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

We can get rid of the TestSourceApp class now, which I think would be nice to flatten this file a bit (if you make this change you could do it in an additional commit if it's easier)

Copy link

Choose a reason for hiding this comment

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

I think we need this class for setup and teardown?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't use those anymore. Lifecycles are managed by pytest alone.

Copy link
Contributor

@redshiftzero redshiftzero left a comment

Choose a reason for hiding this comment

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

Diff looks good, thanks for making those changes! 👍 to merge from me

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Relying on @redshiftzero review. I would dismiss my review if only it was posssible.

@ghost ghost merged commit 7e87330 into develop Mar 12, 2018
@heartsucker heartsucker deleted the pytest-fixtures branch March 12, 2018 16:45
legoktm added a commit that referenced this pull request Feb 3, 2022
We have not used the flask_testing library since <#2948>
(2018), a small part of it was directly copied into SecureDrop.

Plus that version of the library was incompatible with Flask 2.0+ and
by getting rid of it we don't explicitly have to pin Jinja nor markupsafe.
zenmonkeykstop pushed a commit that referenced this pull request Feb 8, 2022
We have not used the flask_testing library since <#2948>
(2018), a small part of it was directly copied into SecureDrop.

Plus that version of the library was incompatible with Flask 2.0+ and
by getting rid of it we don't explicitly have to pin Jinja nor markupsafe.

(cherry picked from commit 65f092c)
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants