How to use test_client method in pytest-django

Best Python code snippet using pytest-django_python

models.py

Source:models.py Github

copy

Full Screen

...387class CustomTestClient(Client):388 i_am_customized = "Yes"389class CustomTestClientTest(TestCase):390 client_class = CustomTestClient391 def test_custom_test_client(self):392 """A test case can specify a custom class for self.client."""393 self.assertEqual(hasattr(self.client, "i_am_customized"), True)394class RequestFactoryTest(TestCase):395 def test_request_factory(self):396 factory = RequestFactory()397 request = factory.get('/somewhere/')398 response = get_view(request)399 self.assertEqual(response.status_code, 200)...

Full Screen

Full Screen

test_views.py

Source:test_views.py Github

copy

Full Screen

1import responses2from fitbit.exceptions import BadResponse3from app.fitbit_client import get_auth_url4from app.models import save_fitbit_token, User5def register_batman(test_client):6 return test_client.post(7 '/register',8 data=dict(9 username='batman',10 password='robin',11 confirm='robin'12 ),13 follow_redirects=True14 )15def login_batman(test_client):16 return test_client.post(17 '/login',18 data=dict(19 username='batman',20 password='robin'21 ),22 follow_redirects=True23 )24def test_unauthed_index(test_client):25 response = test_client.get('/')26 assert b'login' in response.data27def test_unsuccessful_login(test_client):28 response = register_batman(test_client)29 assert response.status_code == 20030 response = test_client.post(31 '/login',32 data=dict(33 username='batman',34 password='joker'35 ),36 follow_redirects=True37 )38 assert response.status_code == 40139 assert b'Invalid Credentials' in response.data40 response = test_client.post(41 '/login',42 data=dict(43 username='joker',44 password='robin'45 ),46 follow_redirects=True47 )48 assert b'Invalid Credentials' in response.data49 assert response.status_code == 40150def test_logout(test_client):51 response = register_batman(test_client)52 assert response.status_code == 20053 response = login_batman(test_client)54 assert response.status_code == 20055 assert b'batman' in response.data56 response = test_client.get('/logout', follow_redirects=True)57 assert b'Logged Out' in response.data58 assert b'login' in response.data # Redirected to login59def test_register_and_login_new_user(test_client):60 response = register_batman(test_client)61 assert response.status_code == 20062 response = login_batman(test_client)63 assert response.status_code == 20064 assert b'batman' in response.data65def test_reregistering_is_error(test_client):66 response = register_batman(test_client)67 assert response.status_code == 20068 response = register_batman(test_client)69 assert response.status_code == 40070 assert b'Username batman already taken' in response.data71@responses.activate72def test_handle_redirect(test_client):73 code = 'iamacode'74 # First we will do the authorization with the code75 # sent76 responses.add(77 responses.POST,78 get_auth_url(code),79 json={'access_token': 'acc', 'refresh_token': 'ref'},80 match_querystring=True81 )82 # Then we will get a token83 responses.add(84 responses.POST,85 'https://api.fitbit.com/oauth2/token',86 json={'access_token': 'acc', 'refresh_token': 'ref'}87 )88 # After getting the token we redirect to index which requests the profile89 responses.add(90 responses.GET,91 'https://api.fitbit.com/1/user/-/profile.json',92 json={'user': {'fullName': 'bat man', 'memberSince': '12/12/12'}}93 )94 register_batman(test_client)95 login_batman(test_client)96 response = test_client.get(97 '/oauth-redirect?code={}'.format(code),98 follow_redirects=True99 )100 assert b'batman' in response.data101 assert b'bat man' in response.data102 assert b'12/12/12' in response.data103@responses.activate104def test_api_call_failed(test_client):105 register_batman(test_client)106 save_fitbit_token(User.query.filter_by(username='batman').first().id, 'acc', 'ref')107 # Then we will get a token108 responses.add(109 responses.POST,110 'https://api.fitbit.com/oauth2/token',111 json={'access_token': 'acc', 'refresh_token': 'ref'}112 )113 # After getting the token we redirect to index which requests the profile114 responses.add(115 responses.GET,116 'https://api.fitbit.com/1/user/-/profile.json',117 body=BadResponse("I FAILED")118 )119 response = login_batman(test_client)...

Full Screen

Full Screen

tests.py

Source:tests.py Github

copy

Full Screen

...5app.config['WTF_CSRF_ENABLED'] = False6app.secret_key = str(uuid.uuid4())7def test_home_oauth_redirect():8 app.config['TESTING'] = False9 test_client = app.test_client()10 rsp = test_client.get('/')11 assert rsp.status == '302 FOUND'12 html = rsp.get_data(as_text=True)13 assert '<a href="/oauth2callback">/oauth2callback</a>' in html14def test_home_page():15 app.config['TESTING'] = True16 test_client = app.test_client()17 rsp = test_client.get('/')18 assert rsp.status == '200 OK'19 html = rsp.get_data(as_text=True)20 assert '<h3>Search for an account to manage delegates for</h3>' in html21def test_search():22 app.config['TESTING'] = True23 test_client = app.test_client()24 rsp = test_client.post('/search', data={'finddelegate': 'test.user@example.com'})25 assert rsp.status == '302 FOUND'26 html = rsp.get_data(as_text=True)27 assert '<a href="/delegate/example.com/test.user">' in html28 rsp = test_client.post('/search', data={'finddelegate': 'test.use'})29 assert rsp.status == '200 OK'30 html = rsp.get_data(as_text=True)31 assert 'Not a valid email address!' in html32def test_add_delegate():33 app.config['TESTING'] = True34 test_client = app.test_client()35 rsp = test_client.post('/delegateto/example.com/danger', data={'newdelegate': 'test.use'})36 assert rsp.status == '302 FOUND'37 html = rsp.get_data(as_text=True)38 assert '<a href="/errgetdelegate/example.com/danger/Not%20a%20valid%20email%20address">/errgetdelegate/example.com/danger/Not%20a%20valid%20email%20address</a>' in html39 rsp = test_client.post('/delegateto/example.com/danger', data={'newdelegate': 'test.user@example.com'})40 assert rsp.status == '200 OK'41 html = rsp.get_data(as_text=True)42 assert 'https://apps-apis.google.com/a/feeds/emailsettings' in html43def test_delete_delegate():44 app.config['TESTING'] = True45 test_client = app.test_client()46 rsp = test_client.get('/delegateto/example.com/test.user/danger')47 assert rsp.status == '200 OK'48 html = rsp.get_data(as_text=True)49 assert 'https://apps-apis.google.com/a/feeds/emailsettings/2.0/example.com/test.user/delegation/danger' in html50def test_err_get_delegate():51 app.config['TESTING'] = True52 test_client = app.test_client()53 rsp = test_client.get('/errgetdelegate/example.com/test.user/testerror')54 assert rsp.status == '200 OK'55 html = rsp.get_data(as_text=True)56 assert 'https://apps-apis.google.com/a/feeds/emailsettings/2.0/example.com/test.user/delegation' in html57def test_get_delegate():58 app.config['TESTING'] = True59 test_client = app.test_client()60 rsp = test_client.get('/delegate/example.com/test.user')61 assert rsp.status == '200 OK'62 html = rsp.get_data(as_text=True)63 assert 'https://apps-apis.google.com/a/feeds/emailsettings/2.0/example.com/test.user/delegation' in html64def test_error():65 app.config['TESTING'] = True66 test_client = app.test_client()67 rsp = test_client.get('/error/TEST')68 assert rsp.status == '200 OK'69 html = rsp.get_data(as_text=True)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pytest-django automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful