Best Python code snippet using slash
test_fixture_mechanism.py
Source:test_fixture_mechanism.py
...356@pytest.fixture357def module_scoped_fixture(store, cleanup_map):358 return _get_scoped_fixture('module', store, cleanup_map)359@pytest.fixture360def session_scoped_fixture(store, cleanup_map):361 return _get_scoped_fixture('session', store, cleanup_map)362def _get_scoped_fixture(scope, store, cleanup_map):363 @store.add_fixture364 @slash.fixture(scope=scope, name='{}_scoped_fixture'.format(scope))365 def fixture(this):366 @this.add_cleanup367 def cleanup():368 cleanup_map[this.name] = True369 return ok(this.name)370 return fixture371def ok(s):...
test_declarativeimplementation.py
Source:test_declarativeimplementation.py
1# Copyright 2013-2020 Reahl Software Services (Pty) Ltd. All rights reserved.2#3# This file is part of Reahl.4#5# Reahl is free software: you can redistribute it and/or modify6# it under the terms of the GNU Affero General Public License as7# published by the Free Software Foundation; version 3 of the License.8#9# This program is distributed in the hope that it will be useful,10# but WITHOUT ANY WARRANTY; without even the implied warranty of11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12# GNU Affero General Public License for more details.13#14# You should have received a copy of the GNU Affero General Public License15# along with this program. If not, see <http://www.gnu.org/licenses/>.16from datetime import datetime, timedelta17import http.cookies18import urllib.parse19from sqlalchemy import Column, ForeignKey, Integer20from webob import Response21from reahl.tofu import scenario, Fixture, uses22from reahl.stubble import stubclass, EmptyStub23from reahl.tofu.pytestsupport import with_fixtures24from reahl.component.context import ExecutionContext25from reahl.sqlalchemysupport import Session, Base, session_scoped26from reahl.webdeclarative.webdeclarative import UserSession, SessionData, UserInput27from reahl.web.ui import Form28from reahl.web_dev.fixtures import WebFixture29from reahl.sqlalchemysupport_dev.fixtures import SqlAlchemyFixture30from reahl.domain_dev.fixtures import PartyAccountFixture31@with_fixtures(WebFixture)32def test_session_active_state(web_fixture):33 """The session is active if the last user interaction was in the last idle_lifetime """34 fixture = web_fixture35 user_session = fixture.context.session36 config = fixture.config37 # Case: recent interaction38 user_session.set_last_activity_time()39 assert user_session.is_active()40 # Case: last interaction not recent41 user_session.last_activity = datetime.now() - timedelta(seconds=user_session.idle_lifetime+10)42 assert not user_session.is_active()43@uses(web_fixture=WebFixture)44class SecureScenarios(Fixture):45 @scenario46 def secure(self):47 self.scheme = 'https'48 self.last_activity = datetime.now()49 self.secure_cookie = self.web_fixture.context.session.secure_salt50 self.expect_secure = True51 @scenario52 def insecure_scheme(self):53 self.secure()54 self.scheme = 'http'55 self.expect_secure = False56 @scenario57 def old_interaction(self):58 self.secure()59 self.last_activity = datetime.now() - timedelta(seconds=self.web_fixture.config.web.idle_secure_lifetime+10)60 self.expect_secure = False61 @scenario62 def bad_cookie(self):63 self.secure()64 self.secure_cookie = 'bad cookie value'65 self.expect_secure = False66@with_fixtures(WebFixture, SecureScenarios)67def test_session_secure_state(web_fixture, secure_scenarios):68 """The session is only secured when used over https, the secure cookie is set correctly,69 and the last interaction is within idle_secure_lifetime"""70 fixture = secure_scenarios71 user_session = web_fixture.context.session72 config = web_fixture.config73 context = web_fixture.context74 assert config.web.idle_secure_lifetime < config.web.idle_lifetime75 assert config.web.idle_lifetime < config.web.idle_lifetime_max76 web_fixture.request.scheme = fixture.scheme77 user_session.last_activity = fixture.last_activity78 context.request.cookies[context.config.web.secure_key_name] = fixture.secure_cookie79 assert user_session.is_secured() is fixture.expect_secure80@with_fixtures(SqlAlchemyFixture, WebFixture)81def test_setting_cookies_on_response(sql_alchemy_fixture, web_fixture):82 """How UserSession sets session and secure cookies in the response."""83 fixture = web_fixture84 @stubclass(UserSession)85 class UserSessionStub(UserSession):86 __tablename__ = 'usersessionstub'87 __mapper_args__ = {'polymorphic_identity': 'usersessionstub'}88 id = Column(Integer, ForeignKey('usersession.id'), primary_key=True)89 secured = False90 def is_secured(self):91 return self.secured92 with sql_alchemy_fixture.persistent_test_classes(UserSessionStub):93 user_session = UserSessionStub()94 class ResponseStub(Response):95 @property96 def cookies(self):97 cookies = http.cookies.SimpleCookie()98 for header, value in self.headerlist:99 if header == 'Set-Cookie':100 cookies.load(value)101 return cookies102 # Case: with an unsecured session, set only the session cookie103 user_session.secured = False104 response = ResponseStub()105 user_session.set_session_key(response)106 session_cookie = response.cookies[fixture.config.web.session_key_name]107 assert session_cookie.value == urllib.parse.quote(user_session.as_key())108 assert session_cookie['path'] == '/'109 assert not session_cookie['max-age']110 #assert 'httponly' in session_cookie111 assert fixture.config.web.secure_key_name not in response.cookies112 # Case: with an secured session, set the session cookie and the secure cookie113 user_session.secured = True114 response = ResponseStub()115 user_session.set_session_key(response)116 assert fixture.config.web.session_key_name in response.cookies117 secure_cookie = response.cookies[fixture.config.web.secure_key_name]118 assert user_session.secure_salt == secure_cookie.value119 assert secure_cookie['path'] == '/'120 assert secure_cookie['max-age'] == '%s' % fixture.config.web.idle_secure_lifetime121 assert 'secure' in secure_cookie122 #assert 'httponly' in secure_cookie123@with_fixtures(WebFixture)124def test_reading_cookies_on_initialising_a_session(web_fixture):125 fixture = web_fixture126 # Case: session cookie not set in Request127 UserSession.initialise_web_session_on(fixture.context)128 assert not fixture.context.session.is_active()129 assert not fixture.context.session.is_secured()130 # Case: session cookie set in Request131 fixture.context.session = None132 user_session = UserSession()133 user_session.set_last_activity_time()134 Session.add(user_session)135 fixture.request.headers['Cookie'] = 'reahl=%s' % user_session.as_key()136 UserSession.initialise_web_session_on(fixture.context)137 assert fixture.context.session is user_session138 assert fixture.context.session.is_active()139 assert not fixture.context.session.is_secured()140 # Case: session cookie set, secure cookie also set in Request, https141 fixture.request.scheme = 'https'142 fixture.context.session = None143 user_session = UserSession()144 user_session.set_last_activity_time()145 Session.add(user_session)146 fixture.request.headers['Cookie'] = 'reahl=%s , reahl_secure=%s' % \147 (user_session.as_key(), user_session.secure_salt)148 UserSession.initialise_web_session_on(fixture.context)149 assert fixture.context.session is user_session150 assert fixture.context.session.is_active()151 assert fixture.context.session.is_secured()152 # Case: session cookie set, secure cookie also set in Request, http153 fixture.request.scheme = 'http'154 fixture.context.session = None155 user_session = UserSession()156 user_session.set_last_activity_time()157 Session.add(user_session)158 fixture.request.headers['Cookie'] = 'reahl=%s , reahl_secure=%s' % \159 (user_session.as_key(), user_session.secure_salt)160 UserSession.initialise_web_session_on(fixture.context)161 assert fixture.context.session is user_session162 assert fixture.context.session.is_active()163 assert not fixture.context.session.is_secured()164@with_fixtures(WebFixture)165def test_session_data_disappears_when_session_does(web_fixture):166 """When a UserSession is deleted, all associated SessionData disappear as well."""167 fixture = web_fixture168 UserSession.initialise_web_session_on(fixture.context)169 user_session = fixture.context.session170 ui_name = 'user_interface'171 channel_name = 'channel'172 session_data = SessionData(web_session=user_session, view_path='/', ui_name=ui_name, channel_name=channel_name)173 Session.add(session_data)174 Session.flush()175 Session.delete(user_session)176 assert Session.query(SessionData).filter_by(id=session_data.id).count() == 0177 assert Session.query(UserSession).filter_by(id=user_session.id).count() == 0178@with_fixtures(WebFixture)179def test_session_keeps_living(web_fixture):180 """When SessionData is deleted, the associated UserSession is not affected."""181 fixture = web_fixture182 UserSession.initialise_web_session_on(fixture.context)183 user_session = fixture.context.session184 ui_name = 'user_interface'185 channel_name = 'channel'186 session_data = SessionData(web_session=user_session, view_path='/', ui_name=ui_name, channel_name=channel_name)187 Session.add(session_data)188 Session.flush()189 Session.delete(session_data)190 assert Session.query(SessionData).filter_by(id=session_data.id).count() == 0191 assert Session.query(UserSession).filter_by(id=user_session.id).one() is user_session192class InputScenarios(Fixture):193 @scenario194 def text(self):195 self.entered_input_type = str196 self.entered_input = 'some value to save'197 self.empty_entered_input = ''198 @scenario199 def lists(self):200 self.entered_input_type = list201 self.entered_input = ['one', 'two']202 self.empty_entered_input = []203@with_fixtures(WebFixture, PartyAccountFixture, InputScenarios)204def test_persisting_input(web_fixture, party_account_fixture, input_scenarios):205 """UserInput can persist and find user input entered as a string or a list of strings (useful for things206 like multiple select boxes or multiple checkboxes with the same name).207 """208 @stubclass(Form)209 class FormStub:210 view = web_fixture.view211 user_interface = EmptyStub(name='myui')212 channel_name = 'myform'213 fixture = input_scenarios214 form = FormStub()215 # If never persisted, getting it returns None216 previously_entered = UserInput.get_previously_entered_for_form(form, 'aninput', fixture.entered_input_type)217 assert previously_entered is None218 # Once persisted, getting it returns what you persisted219 UserInput.save_input_value_for_form(form, 'aninput', fixture.entered_input, fixture.entered_input_type)220 previously_entered = UserInput.get_previously_entered_for_form(form, 'aninput', fixture.entered_input_type)221 assert previously_entered == fixture.entered_input222 # Persisting empty values returns them correctly, not None (None means was not entered previously)223 UserInput.clear_for_form(form)224 previously_entered = UserInput.get_previously_entered_for_form(form, 'aninput', fixture.entered_input_type)225 assert previously_entered is None226 UserInput.save_input_value_for_form(form, 'aninput', fixture.empty_entered_input, fixture.entered_input_type)227 previously_entered = UserInput.get_previously_entered_for_form(form, 'aninput', fixture.entered_input_type)228 assert previously_entered == fixture.empty_entered_input229class SessionScopedFixture(Fixture):230 def create_user_session(self):231 user_session = UserSession()232 Session.add(user_session)233 ExecutionContext.get_context().session = user_session234 return user_session235 @scenario236 def remove_session_scoped(self):237 self.expected_user_session_after_delete = 1238 self.cascade = False239 @scenario240 def remove_user_session(self):241 self.expected_user_session_after_delete = 0242 self.cascade = True243@with_fixtures(SqlAlchemyFixture, SessionScopedFixture)244def test_cascade_removal_of_user_session(sql_alchemy_fixture, session_scoped_fixture):245 """246 If a user session is deleted, all session scoped objects are deleted as well.247 """248 fixture = session_scoped_fixture249 @session_scoped250 class MySessionScoped(Base):251 __tablename__ = 'my_session_scoped'252 id = Column(Integer, primary_key=True)253 with sql_alchemy_fixture.persistent_test_classes(MySessionScoped):254 user_session = fixture.create_user_session()255 assert Session.query(MySessionScoped).count() == 0256 session_object = MySessionScoped.for_current_session()257 assert Session.query(MySessionScoped).one() is session_object258 assert session_object.user_session is user_session259 if fixture.cascade:260 Session.delete(user_session)261 else:262 Session.delete(session_object)263 Session.flush()264 assert Session.query(MySessionScoped).count() == 0...
test_xdist.py
Source:test_xdist.py
1import time2import pytest3# пÑи паÑаллелÑном запÑÑке session scope ÑикÑÑÑÑа бÑÐ´ÐµÑ Ð²Ñзвана много Ñаз4@pytest.fixture(scope="session", autouse=True)5def session_scoped_fixture():6 pass7# паÑамеÑÑизаÑÐ¸Ñ Ð½Ð° 20 ÑеÑÑов8# [gw0] [100%] PASSED test_xdist.py::test_many_params[10]9@pytest.mark.parametrize("param", range(20))10def test_many_params(param, session_scoped_fixture):...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!