-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
use setUpClass&tearDownClass #1240
-
hi,
If I want to perform the use case with the following way, I should handle it, only SET UP and TEARDOWN methods.
class framework_test(BaseCase):
@classmethod
def setUpClass(cls) -> None:
Beta Was this translation helpful? Give feedback.
All reactions
If you want to customize setUp()
and tearDown()
, do it like this:
https://github.com/seleniumbase/SeleniumBase/blob/master/examples/boilerplates/base_test_case.py
Example:
from seleniumbase import BaseCase class BaseTestCase(BaseCase): def setUp(self): super(BaseTestCase, self).setUp() # <<< Run custom setUp() code for tests AFTER the super().setUp() >>> def tearDown(self): self.save_teardown_screenshot() if self.has_exception(): # <<< Run custom code if the test failed. >>> pass else: # <<< Run custom code if the test passed. >>> pass # (Wrap unreliable tearDown() code in a try/excep...
Replies: 2 comments 1 reply
-
If you want to customize setUp()
and tearDown()
, do it like this:
https://github.com/seleniumbase/SeleniumBase/blob/master/examples/boilerplates/base_test_case.py
Example:
from seleniumbase import BaseCase class BaseTestCase(BaseCase): def setUp(self): super(BaseTestCase, self).setUp() # <<< Run custom setUp() code for tests AFTER the super().setUp() >>> def tearDown(self): self.save_teardown_screenshot() if self.has_exception(): # <<< Run custom code if the test failed. >>> pass else: # <<< Run custom code if the test passed. >>> pass # (Wrap unreliable tearDown() code in a try/except block.) # <<< Run custom tearDown() code BEFORE the super().tearDown() >>> super(BaseTestCase, self).tearDown() def login(self): # <<< Placeholder. Add your code here. >>> # Reduce duplicate code in tests by having reusable methods like this. # If the UI changes, the fix can be applied in one place. pass def example_method(self): # <<< Placeholder. Add your code here. >>> pass class MyTests(BaseTestCase): def test_example(self): self.login() self.example_method()
Beta Was this translation helpful? Give feedback.
All reactions
-
sir,I want to use this way to run case,How should I handle?
class framework_test(driver_handler):
@classmethod
def setUpClass(cls) -> None:
driver1 = cls.get_new_driver()
cls.switch_to_driver(driver1)
cls.open('https://www.google.com.hk/')
Beta Was this translation helpful? Give feedback.
All reactions
-
SeleniumBase can be invoked in two main ways:
- Via the
unittest.TestCase
structure by subclassing BaseCase. (Runs withpytest
) - Via the
sb
pytest fixture.
All the syntax formats from https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/syntax_formats.md utilize these two main ways.
This allows for some flexibility, but not complete flexibility. The way you're trying to invoke SeleniumBase would bypass unittest's overridden setUp()
and tearDown()
methods that are specific for SeleniumBase. unittest
comes with Python, and that structure can't really be changed.
Instead, you could invoke a custom setup method from within your test. Here's how to do that using the sb
pytest fixture:
class LoginPage: def login_to_swag_labs(self, sb, username): sb.open("https://www.saucedemo.com") sb.type("#user-name", username) sb.type("#password", "secret_sauce") sb.click('input[type="submit"]') class MyTests: def test_swag_labs_login(self, sb): LoginPage().login_to_swag_labs(sb, "standard_user") sb.assert_element("#inventory_container") sb.assert_element('div:contains("Sauce Labs Backpack")')
In summary, SeleniumBase just extends the structure already provided by unittest.TestCase
and pytest
fixtures. Those are set, and SeleniumBase has to follow their rules unless we build our own completely different structure. Those allow for some flexibility, and a list of the current 17 syntax formats are located here: https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/syntax_formats.md
Beta Was this translation helpful? Give feedback.