Keine Beschreibung

conftest.py 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # -*- encoding: utf-8 -*-
  2. """
  3. License: MIT
  4. Copyright (c) 2019 - present AppSeed.us
  5. """
  6. from config import config_dict
  7. from pytest import fixture
  8. from selenium import webdriver
  9. from selenium.webdriver.chrome.options import Options
  10. from threading import Thread
  11. from time import sleep
  12. from app import create_app, db
  13. @fixture
  14. def base_client():
  15. app = create_app(config_dict['Debug'])
  16. app_ctx = app.app_context()
  17. app_ctx.push()
  18. db.session.close()
  19. db.drop_all()
  20. yield app.test_client()
  21. @fixture
  22. def user_client():
  23. app = create_app(config_dict['Debug'])
  24. app_ctx = app.app_context()
  25. app_ctx.push()
  26. db.session.close()
  27. db.drop_all()
  28. client = app.test_client()
  29. create = {'username': '', 'password': '', 'create_account': ''}
  30. login = {'username': '', 'password': '', 'login': ''}
  31. with app.app_context():
  32. client.post('/create_user', data=create)
  33. client.post('/login', data=login)
  34. yield client
  35. @fixture
  36. def selenium_client():
  37. app = create_app(config_dict['Debug'], True)
  38. app_context = app.app_context()
  39. app_context.push()
  40. db.session.close()
  41. db.drop_all()
  42. options = Options()
  43. options.add_argument('--headless')
  44. # options.add_argument('--disable-gpu')
  45. # Flask can run in a separate thread, but the reloader expects to run in
  46. # the main thread: it must be disabled
  47. client = None
  48. try:
  49. client = webdriver.Chrome('./tests/chromedriver', chrome_options=options)
  50. except Exception:
  51. pass
  52. # if the client cannot start, we don't want to start a Thread as the
  53. # test execution would be stuck
  54. if client:
  55. Thread(
  56. target=app.run,
  57. kwargs={
  58. 'host': '0.0.0.0',
  59. 'port': 5000,
  60. 'use_reloader': False
  61. }
  62. ).start()
  63. # give the server some time to start
  64. sleep(1)
  65. yield client
  66. client.get('http://127.0.0.1:5000/shutdown')
  67. client.quit()
  68. app_context.pop()