Best Python code snippet using autotest_python
conftest.py
Source:conftest.py
...8@pytest.fixture(autouse=True, scope="session")9def session_clean_up() -> NoReturn:10 """Mandatory tests environment clean up before/after test session."""11 client = MongoClient("localhost", 27017)12 client.drop_database("flask_mongoengine_test_db")13 client.drop_database("flask_mongoengine_test_db_1")14 client.drop_database("flask_mongoengine_test_db_2")15 yield16 client.drop_database("flask_mongoengine_test_db")17 client.drop_database("flask_mongoengine_test_db_1")18 client.drop_database("flask_mongoengine_test_db_2")19@pytest.fixture()20def app() -> Flask:21 app = Flask(__name__)22 app.config["TESTING"] = True23 app.config["WTF_CSRF_ENABLED"] = False24 with app.app_context():25 yield app26 mongoengine.connection.disconnect_all()27@pytest.fixture()28def db(app) -> MongoEngine:29 app.config["MONGODB_SETTINGS"] = [30 {31 "db": "flask_mongoengine_test_db",32 "host": "localhost",33 "port": 27017,34 "alias": "default",35 "uuidRepresentation": "standard",36 }37 ]38 test_db = MongoEngine(app)39 db_name = (40 test_db.connection["default"].get_database("flask_mongoengine_test_db").name41 )42 if not db_name.endswith("_test_db"):43 raise RuntimeError(44 f"DATABASE_URL must point to testing db, not to master db ({db_name})"45 )46 # Clear database before tests, for cases when some test failed before.47 test_db.connection["default"].drop_database(db_name)48 yield test_db49 # Clear database after tests, for graceful exit.50 test_db.connection["default"].drop_database(db_name)51@pytest.fixture()52def todo(db):53 class Todo(db.Document):54 title = mongoengine.StringField(max_length=60)55 text = mongoengine.StringField()56 done = mongoengine.BooleanField(default=False)57 pub_date = mongoengine.DateTimeField(default=datetime.utcnow)58 comments = mongoengine.ListField(mongoengine.StringField())59 comment_count = mongoengine.IntField()...
__init__.py
Source:__init__.py
...38 except ConnectionFailure:39 # Tests where ssl=True can cause connection failures here.40 # Ignore and continue.41 return42 c.drop_database("pymongo-pooling-tests")43 c.drop_database("pymongo_test")44 c.drop_database("pymongo_test1")45 c.drop_database("pymongo_test2")46 c.drop_database("pymongo_test_mike")...
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!!