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

test: Start integrating pytest #6827

Merged
merged 16 commits into from May 7, 2020
Merged

test: Start integrating pytest #6827

merged 16 commits into from May 7, 2020

Conversation

me-diru
Copy link
Contributor

@me-diru me-diru commented Feb 9, 2020

Reference #6503

Short description of what this resolves:

Converts test code to pytest standard

Changes proposed in this pull request:

Change in test code

Checklist

  • I have read the Contribution & Best practices Guide and my PR follows them.
  • My branch is up-to-date with the Upstream development branch.
  • The unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • All the functions created/modified in this PR contain relevant docstrings.

"""To test details in the form of dict"""
error_response = ErrorResponse(source="test source", detail="test detail")
expected_dict = {
'status': error_response.status,

Choose a reason for hiding this comment

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

Black would make changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pre-commit ran black

@codecov
Copy link

codecov bot commented Feb 9, 2020

Codecov Report

Merging #6827 into development will decrease coverage by 6.07%.
The diff coverage is n/a.

Impacted file tree graph

@@               Coverage Diff               @@
##           development    #6827      +/-   ##
===============================================
- Coverage        66.94%   60.87%   -6.08%     
===============================================
  Files              314      259      -55     
  Lines            15423    13564    -1859     
===============================================
- Hits             10325     8257    -2068     
- Misses            5098     5307     +209     
Impacted Files Coverage Δ
app/api/helpers/scheduled_jobs.py 48.29% <ø> (ø)
app/models/event.py 81.27% <ø> (ø)
populate_db.py 90.52% <ø> (ø)
app/factories/microlocation.py
tests/all/integration/auth_helper.py
tests/all/integration/api/helpers/test_files.py
...ntegration/api/helpers/test_systemnotifications.py
...all/integration/api/helpers/test_scheduled_jobs.py
app/factories/export_job.py
tests/all/unit/graphql/utils/test_fields.py
... and 64 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 41adf3e...f1abace. Read the comment docs.

mrsaicharan1
mrsaicharan1 previously approved these changes Feb 9, 2020
Copy link
Member

@mrsaicharan1 mrsaicharan1 left a comment

Choose a reason for hiding this comment

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

Looks fine to me

@me-diru
Copy link
Contributor Author

me-diru commented Feb 9, 2020

@iamareebjamal Can you please take a look at this?

@iamareebjamal
Copy link
Member

This will close the issue when it just changes 1 test. Besides, the main tests we want to change are db tests

@me-diru
Copy link
Contributor Author

me-diru commented Feb 9, 2020

This will close the issue when it just changes 1 test. Besides, the main tests we want to change are db tests

Yes, I just wanted to know if I was going in the right direction on this. I am planning to change all the tests in a similar way in this PR itself.

@iamareebjamal
Copy link
Member

Just change 1 DB test for now


user = create_user(email='authtest2@gmail.com', password='password')
user.is_admin = False
status = AuthManager.check_auth_admin('authtest2@gmail.com', 'password')
self.assertEqual(False, status)
assert False == status

Choose a reason for hiding this comment

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

comparison to False should be 'if cond is False:' or 'if not cond:'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

doesn't apply here.

@@ -42,12 +42,12 @@ def test_check_auth_admin(self):
user = create_user(email='authtest@gmail.com', password='password')
user.is_admin = True
status = AuthManager.check_auth_admin('authtest@gmail.com', 'password')
self.assertEqual(True, status)
assert True == status

Choose a reason for hiding this comment

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

comparison to True should be 'if cond is True:' or 'if cond:'

@@ -33,7 +33,7 @@ def test_is_accessible(self):
user = create_user(email='test@test.com', password='password')
login_user(user)
logout_user()
self.assertEqual(AuthManager.is_accessible(), False)
assert AuthManager.is_accessible() == False

Choose a reason for hiding this comment

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

comparison to False should be 'if cond is False:' or 'if not cond:'

@@ -24,7 +24,7 @@ def test_verified_user(self):
user = create_user(email='authtest@gmail.com', password='password')
user.is_verified = False
login_user(user)
self.assertEqual(AuthManager.is_verified_user(), False)
assert AuthManager.is_verified_user() == False

Choose a reason for hiding this comment

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

comparison to False should be 'if cond is False:' or 'if not cond:'

@@ -15,7 +15,7 @@ def test_load_user(self):

with self.app.test_request_context():
user = create_user(email='authtest@gmail.com', password='password')
self.assertEqual(user, db.session.query(User).get(user.id))
assert user == db.session.query(User).get(user.id)
Copy link
Member

Choose a reason for hiding this comment

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

Nope. It is still not pytest test. This is standard unit test

user = create_user(email='authtest2@gmail.com', password='password')
user.is_admin = False
status = AuthManager.check_auth_admin('authtest2@gmail.com', 'password')
assert False == status

Choose a reason for hiding this comment

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

comparison to False should be 'if cond is False:' or 'if not cond:'

user = create_user(email='authtest@gmail.com', password='password')
user.is_admin = True
status = AuthManager.check_auth_admin('authtest@gmail.com', 'password')
assert True == status

Choose a reason for hiding this comment

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

comparison to True should be 'if cond is True:' or 'if cond:'

user = create_user(email='test@test.com', password='password')
login_user(user)
logout_user()
assert AuthManager.is_accessible() == False

Choose a reason for hiding this comment

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

comparison to False should be 'if cond is False:' or 'if not cond:'

user = create_user(email='authtest@gmail.com', password='password')
user.is_verified = False
login_user(user)
assert AuthManager.is_verified_user() == False

Choose a reason for hiding this comment

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

comparison to False should be 'if cond is False:' or 'if not cond:'

from tests.all.integration.setup_database import Setup


@pytest.fixture(scope='module')

Choose a reason for hiding this comment

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

Black would make changes.

from flask_login import login_user, logout_user

from flask import Flask, session

Choose a reason for hiding this comment

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

'flask.Flask' imported but unused
'flask.session' imported but unused
redefinition of unused 'Flask' from line 2

@me-diru
Copy link
Contributor Author

me-diru commented Feb 13, 2020

@iamareebjamal @mrsaicharan1 can you please take a look at this?

@iamareebjamal
Copy link
Member

Yes, I did. And the build is failing

@me-diru
Copy link
Contributor Author

me-diru commented Feb 13, 2020

image

The tests are passing locally when I run using pytest

@me-diru
Copy link
Contributor Author

me-diru commented Feb 14, 2020

@iamareebjamal Can you please review this?
I think it's failing because travis build runs nosetest command.

@iamareebjamal
Copy link
Member

Then change it to correct command

@me-diru
Copy link
Contributor Author

me-diru commented Feb 16, 2020

do I have to add pytest in requirements and change travis yml file ?

@iamareebjamal
Copy link
Member

You may

.travis.yml Outdated
- nosetests tests/ -v --with-coverage
script: |
nosetests tests/ -v --with-coverage -I tests/all/integration/api/helpers/test_auth.py
pytest tests/all/integration/api/helpers/test_auth.py
Copy link
Member

Choose a reason for hiding this comment

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

Not cool

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 didn't know another way to do it.
Somehow I have to exclude test_auth from nostests and test it with pytest

.travis.yml Outdated
@@ -40,3 +41,4 @@ branches:
only:
- master
- development
- pytest
Copy link
Member

Choose a reason for hiding this comment

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

Not cool

.travis.yml Outdated
script:
- nosetests tests/ -v --with-coverage
script:
- nosetests tests/ --ignore-files="test_auth\.py" -v --with-coverage
Copy link
Member

Choose a reason for hiding this comment

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

Nope

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sir, nosetests fails for a pytest code. Should I add a pytest script in yml file?

Copy link
Member

Choose a reason for hiding this comment

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

All tests should run with 1 script

user = create_user(email='authtest2@gmail.com', password='password')
user.is_admin = False
status = AuthManager.check_auth_admin('authtest2@gmail.com', 'password')
assert False == status

Choose a reason for hiding this comment

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

comparison to False should be 'if cond is False:' or 'if not cond:'

user = create_user(email='authtest1@gmail.com', password='password')
user.is_admin = True
status = AuthManager.check_auth_admin('authtest1@gmail.com', 'password')
assert True == status

Choose a reason for hiding this comment

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

comparison to True should be 'if cond is True:' or 'if cond:'

user = create_user(email='test@test.com', password='password')
login_user(user)
logout_user()
assert AuthManager.is_accessible() == False

Choose a reason for hiding this comment

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

comparison to False should be 'if cond is False:' or 'if not cond:'

user = create_user(email='authtest@gmail.com', password='password')
user.is_verified = False
login_user(user)
assert AuthManager.is_verified_user() == False

Choose a reason for hiding this comment

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

comparison to False should be 'if cond is False:' or 'if not cond:'

@niranjan94
Copy link
Member

Codacy Here is an overview of what got changed by this pull request:

Complexity increasing per file
==============================
- tests/all/integration/api/validation/test_created_at.py  5
- tests/all/unit/api/helpers/test_errors.py  5
- tests/all/integration/api/helpers/test_auth.py  1
         

See the complete overview on Codacy

@iamareebjamal iamareebjamal changed the title test : Changed test_erros to pytest test : Start integrating pytest May 7, 2020
@iamareebjamal iamareebjamal changed the title test : Start integrating pytest test: Start integrating pytest May 7, 2020
@iamareebjamal iamareebjamal merged commit 82aab77 into fossasia:development May 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants