conftest.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright 2021-2025 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import os
  12. from pathlib import Path
  13. from unittest.mock import patch
  14. import pandas as pd # type: ignore
  15. import pytest
  16. from flask import Flask
  17. from taipy.gui.servers import get_request_meta, get_server_type
  18. csv = pd.read_csv(
  19. f"{Path(Path(__file__).parent.resolve())}{os.path.sep}current-covid-patients-hospital.csv", parse_dates=["Day"]
  20. )
  21. small_dataframe_data = {"name": ["A", "B", "C"], "value": [1, 2, 3]}
  22. @pytest.fixture(scope="function")
  23. def csvdata():
  24. yield csv
  25. @pytest.fixture(scope="function")
  26. def small_dataframe():
  27. yield small_dataframe_data
  28. @pytest.fixture(scope="function")
  29. def gui(helpers, gui_server):
  30. from taipy.gui import Gui
  31. gui = Gui(server=gui_server)
  32. yield gui
  33. # Delete Gui instance and state of some classes after each test
  34. gui.stop()
  35. helpers.test_cleanup()
  36. @pytest.fixture
  37. def helpers():
  38. from .helpers import Helpers
  39. return Helpers
  40. @pytest.fixture
  41. def test_client():
  42. if get_server_type() == "flask":
  43. flask_app = Flask("Test App")
  44. # Create a test client using the Flask application configured for testing
  45. with flask_app.test_client() as testing_client:
  46. # Establish an application context
  47. with flask_app.app_context():
  48. get_request_meta().client_id = "test client id"
  49. yield testing_client # this is where the testing happens!
  50. else:
  51. yield None
  52. @pytest.fixture(scope="function", autouse=True)
  53. def patch_cli_args():
  54. with patch("sys.argv", ["prog"]):
  55. yield
  56. @pytest.fixture(autouse=True)
  57. def skip_if_gui_server_is_not(request, gui_server):
  58. skip_marker = request.node.get_closest_marker("skip_if_not_server")
  59. if skip_marker and skip_marker.args[0] != gui_server:
  60. pytest.skip(f"Skipped because gui_server is not {gui_server}")