From 5d42f68d5f3ef3e54037c5fee29708525526edbf Mon Sep 17 00:00:00 2001 From: Joshua Fehler Date: Tue, 4 Jun 2024 09:41:00 -0400 Subject: [PATCH] tests: Remove unittest usage --- tests/form_elements.py | 9 ++++---- tests/test_djangoclient.py | 16 ++++++-------- tests/test_flaskclient.py | 31 +++++++++++---------------- tests/test_request_handler.py | 18 +++++++++------- tests/test_webdriver_chrome.py | 6 ++---- tests/test_webdriver_edge_chromium.py | 6 ++---- tests/test_webdriver_firefox.py | 5 ++--- tests/test_webdriver_remote.py | 16 ++++++++------ tests/test_zopetestbrowser.py | 18 +++++++--------- 9 files changed, 56 insertions(+), 69 deletions(-) diff --git a/tests/form_elements.py b/tests/form_elements.py index 63da41711..98a89e146 100644 --- a/tests/form_elements.py +++ b/tests/form_elements.py @@ -3,7 +3,6 @@ # license that can be found in the LICENSE file. import re import time -import unittest import pytest @@ -12,8 +11,8 @@ def skip_if_zope(f): def wrapper(self, *args, **kwargs): - if self.__class__.__name__ == "ZopeTestBrowserDriverTest": - return unittest.skip("skipping this test for zope testbrowser") + if self.__class__.__name__ == "TestZopeTestBrowserDriver": + return pytest.skip("skipping this test for zope testbrowser") else: f(self, *args, **kwargs) @@ -22,8 +21,8 @@ def wrapper(self, *args, **kwargs): def skip_if_django(f): def wrapper(self, *args, **kwargs): - if self.__class__.__name__ == "DjangoClientDriverTest": - return unittest.skip("skipping this test for django") + if self.__class__.__name__ == "TestDjangoClientDriver": + return pytest.skip("skipping this test for django") else: f(self, *args, **kwargs) diff --git a/tests/test_djangoclient.py b/tests/test_djangoclient.py index 8263af2a7..0ae818642 100644 --- a/tests/test_djangoclient.py +++ b/tests/test_djangoclient.py @@ -4,7 +4,6 @@ import os import sys import time -import unittest import django import pytest @@ -22,7 +21,7 @@ django.setup() -class DjangoClientDriverTest(BaseBrowserTests, unittest.TestCase): +class TestDjangoClientDriver(BaseBrowserTests): @pytest.fixture(autouse=True, scope="class") def setup_browser(self, request): request.cls.browser = get_browser("django") @@ -150,21 +149,18 @@ def test_cookies_extra_parameters(self): assert timestamp == cookie["expires"] -class DjangoClientDriverTestWithCustomHeaders(unittest.TestCase): - @classmethod - def setUpClass(cls): +class TestDjangoClientDriverWithCustomHeaders: + @pytest.fixture(autouse=True, scope="class") + def setup_browser(self, request): custom_headers = { "X-Splinter-Customheaders-1": "Hello", "X-Splinter-Customheaders-2": "Bye", } - cls.browser = Browser("django", custom_headers=custom_headers) + request.cls.browser = get_browser("django", custom_headers=custom_headers) + request.addfinalizer(request.cls.browser.quit) def test_create_a_phantomjs_with_custom_headers(self): self.browser.visit(EXAMPLE_APP + "headers") assert self.browser.is_text_present("X-Splinter-Customheaders-1: Hello") assert self.browser.is_text_present("X-Splinter-Customheaders-2: Bye") - - @classmethod - def tearDownClass(cls): - cls.browser.quit() diff --git a/tests/test_flaskclient.py b/tests/test_flaskclient.py index 9c1e80c83..78ff1b320 100644 --- a/tests/test_flaskclient.py +++ b/tests/test_flaskclient.py @@ -3,28 +3,26 @@ # license that can be found in the LICENSE file. import os import time -import unittest import pytest from .base import BaseBrowserTests +from .base import get_browser from .fake_webapp import app from .fake_webapp import EXAMPLE_APP from splinter import Browser -class FlaskClientDriverTest(BaseBrowserTests, unittest.TestCase): - @classmethod - def setUpClass(cls): - cls.browser = Browser("flask", app=app, wait_time=0.1) +class TestFlaskClientDriver(BaseBrowserTests): + @pytest.fixture(autouse=True, scope="class") + def setup_browser(self, request): + request.cls.browser = get_browser("flask", app=app, wait_time=0.1) + request.addfinalizer(request.cls.browser.quit) - def setUp(self): + @pytest.fixture(autouse=True) + def visit_example_app(self, request): self.browser.visit(EXAMPLE_APP) - @classmethod - def tearDownClass(cls): - cls.browser.quit() - def test_should_support_with_statement(self): with Browser("flask", app=app) as internet: assert internet is not None @@ -174,20 +172,17 @@ def test_cookies_extra_parameters(self): assert timestamp == int(cookie.expires.timestamp()) -class FlaskClientDriverTestWithCustomHeaders(unittest.TestCase): - @classmethod - def setUpClass(cls): +class TestFlaskClientDriverWithCustomHeaders: + @pytest.fixture(autouse=True, scope="class") + def setup_browser(self, request): custom_headers = { "X-Splinter-Customheaders-1": "Hello", "X-Splinter-Customheaders-2": "Bye", } - cls.browser = Browser("flask", app=app, custom_headers=custom_headers) + request.cls.browser = get_browser("flask", app=app, custom_headers=custom_headers) + request.addfinalizer(request.cls.browser.quit) def test_create_a_flask_client_with_custom_headers(self): self.browser.visit(EXAMPLE_APP + "headers") assert self.browser.is_text_present("X-Splinter-Customheaders-1: Hello") assert self.browser.is_text_present("X-Splinter-Customheaders-2: Bye") - - @classmethod - def tearDownClass(cls): - cls.browser.quit() diff --git a/tests/test_request_handler.py b/tests/test_request_handler.py index 0b06524c6..ae0773fff 100644 --- a/tests/test_request_handler.py +++ b/tests/test_request_handler.py @@ -1,17 +1,19 @@ # Copyright 2012 splinter authors. All rights reserved. # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. -import unittest +import pytest from splinter.request_handler.status_code import StatusCode -class RequestHandlerTestCase(unittest.TestCase): - def setUp(self): - self.status_code = StatusCode(200, "OK") +@pytest.fixture() +def valid_status_code(): + return StatusCode(200, "OK") - def test_should_receive_an_url_and_get_a_success_response(self): - assert self.status_code.is_success() - def test_should_compare_app_index_with_404_and_get_false(self): - assert self.status_code != 404 +def test_should_receive_an_url_and_get_a_success_response(valid_status_code): + assert valid_status_code.is_success() + + +def test_should_compare_app_index_with_404_and_get_false(valid_status_code): + assert valid_status_code != 404 diff --git a/tests/test_webdriver_chrome.py b/tests/test_webdriver_chrome.py index e69f4e886..07426abc7 100644 --- a/tests/test_webdriver_chrome.py +++ b/tests/test_webdriver_chrome.py @@ -1,8 +1,6 @@ # Copyright 2013 splinter authors. All rights reserved. # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. -import unittest - import pytest from .base import get_browser @@ -10,7 +8,7 @@ from .fake_webapp import EXAMPLE_APP -class ChromeBrowserTest(WebDriverTests, unittest.TestCase): +class TestChromeBrowser(WebDriverTests): @pytest.fixture(autouse=True, scope="class") def setup_browser(self, request): request.cls.browser = get_browser("chrome", fullscreen=False) @@ -22,7 +20,7 @@ def visit_example_app(self, request): self.browser.visit(EXAMPLE_APP) -class ChromeBrowserFullscreenTest(WebDriverTests, unittest.TestCase): +class TestChromeBrowserFullscreen(WebDriverTests): @pytest.fixture(autouse=True, scope="class") def setup_browser(self, request): request.cls.browser = get_browser("chrome", fullscreen=True) diff --git a/tests/test_webdriver_edge_chromium.py b/tests/test_webdriver_edge_chromium.py index 684702d28..b88b6d004 100644 --- a/tests/test_webdriver_edge_chromium.py +++ b/tests/test_webdriver_edge_chromium.py @@ -1,8 +1,6 @@ # Copyright 2021 splinter authors. All rights reserved. # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. -import unittest - import pytest from .base import get_browser @@ -10,7 +8,7 @@ from .fake_webapp import EXAMPLE_APP -class EdgeChromiumBrowserTest(WebDriverTests, unittest.TestCase): +class TestEdgeChromiumBrowser(WebDriverTests): @pytest.fixture(autouse=True, scope="class") def setup_browser(self, request): request.cls.browser = get_browser("edge", fullscreen=False) @@ -22,7 +20,7 @@ def visit_example_app(self, request): self.browser.visit(EXAMPLE_APP) -class EdgeChromiumBrowserFullscreenTest(WebDriverTests, unittest.TestCase): +class TestEdgeChromiumBrowserFullscreen(WebDriverTests): @pytest.fixture(autouse=True, scope="class") def setup_browser(self, request): request.cls.browser = get_browser("edge", fullscreen=True) diff --git a/tests/test_webdriver_firefox.py b/tests/test_webdriver_firefox.py index 47ec59449..6debe3828 100644 --- a/tests/test_webdriver_firefox.py +++ b/tests/test_webdriver_firefox.py @@ -2,7 +2,6 @@ # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. import os -import unittest import pytest @@ -12,7 +11,7 @@ from splinter.config import Config -class FirefoxBrowserTest(WebDriverTests, unittest.TestCase): +class TestFirefoxBrowser(WebDriverTests): @pytest.fixture(autouse=True, scope="class") def setup_browser(self, request): request.cls.browser = get_browser("firefox", fullscreen=False) @@ -23,7 +22,7 @@ def visit_example_app(self, request): self.browser.visit(EXAMPLE_APP) -class FirefoxBrowserFullScreenTest(WebDriverTests, unittest.TestCase): +class TestFirefoxBrowserFullScreen(WebDriverTests): @pytest.fixture(autouse=True, scope="class") def setup_browser(self, request): request.cls.browser = get_browser("firefox", fullscreen=True) diff --git a/tests/test_webdriver_remote.py b/tests/test_webdriver_remote.py index 4b0f9beba..239bf77de 100644 --- a/tests/test_webdriver_remote.py +++ b/tests/test_webdriver_remote.py @@ -1,7 +1,6 @@ # Copyright 2013 splinter authors. All rights reserved. # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. -import unittest from unittest.mock import patch from urllib.request import urlopen @@ -22,13 +21,14 @@ def selenium_server_is_running(): return "WebDriver Hub" in page_contents -class RemoteBrowserFirefoxTest(WebDriverTests, unittest.TestCase): +class TestRemoteBrowserFirefox(WebDriverTests): @pytest.fixture(autouse=True, scope="class") def setup_browser(self, request): request.cls.browser = Browser("remote", browser="firefox") request.addfinalizer(request.cls.browser.quit) - def setUp(self): + @pytest.fixture(autouse=True) + def visit_example_app(self, request): self.browser.visit(EXAMPLE_APP) def test_support_with_statement(self): @@ -41,13 +41,14 @@ def test_should_be_able_to_change_user_agent(self): pass -class RemoteBrowserChromeTest(WebDriverTests, unittest.TestCase): +class TestRemoteBrowserChrome(WebDriverTests): @pytest.fixture(autouse=True, scope="class") def setup_browser(self, request): request.cls.browser = Browser("remote", browser="chrome") request.addfinalizer(request.cls.browser.quit) - def setUp(self): + @pytest.fixture(autouse=True) + def visit_example_app(self, request): self.browser.visit(EXAMPLE_APP) def test_support_with_statement(self): @@ -61,7 +62,7 @@ def test_should_be_able_to_change_user_agent(self): @pytest.mark.macos -class RemoteBrowserSafariTest(WebDriverTests, unittest.TestCase): +class TestRemoteBrowserSafari(WebDriverTests): @pytest.fixture(autouse=True, scope="class") def setup_browser(self, request): # test with statement. It can't be used as simple test @@ -72,7 +73,8 @@ def setup_browser(self, request): request.cls.browser = Browser("remote", browser="safari") request.addfinalizer(request.cls.browser.quit) - def setUp(self): + @pytest.fixture(autouse=True) + def visit_example_app(self, request): self.browser.visit(EXAMPLE_APP) def test_support_with_statement(self): diff --git a/tests/test_zopetestbrowser.py b/tests/test_zopetestbrowser.py index 87c28b5d0..ed819cfb8 100644 --- a/tests/test_zopetestbrowser.py +++ b/tests/test_zopetestbrowser.py @@ -2,27 +2,25 @@ # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. import os -import unittest import pytest from .base import BaseBrowserTests +from .base import get_browser from .fake_webapp import EXAMPLE_APP from splinter import Browser -class ZopeTestBrowserDriverTest(BaseBrowserTests, unittest.TestCase): - @classmethod - def setUpClass(cls): - cls.browser = Browser("zope.testbrowser", wait_time=0.1) +class TestZopeTestBrowserDriver(BaseBrowserTests): + @pytest.fixture(autouse=True, scope="class") + def setup_browser(self, request): + request.cls.browser = get_browser("zope.testbrowser", wait_time=0.1) + request.addfinalizer(request.cls.browser.quit) - def setUp(self): + @pytest.fixture(autouse=True) + def visit_example_app(self, request): self.browser.visit(EXAMPLE_APP) - @classmethod - def tearDownClass(cls): - cls.browser.quit() - def test_should_support_with_statement(self): with Browser("zope.testbrowser"): pass