Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

use setUpClass&tearDownClass #1240

Answered by mdmintz
c350147221 asked this question in Q&A
Discussion options

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:
You must be logged in to vote

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

Comment options

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()
You must be logged in to vote
0 replies
Answer selected by mdmintz
Comment options

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/')
You must be logged in to vote
1 reply
Comment options

SeleniumBase can be invoked in two main ways:

  1. Via the unittest.TestCase structure by subclassing BaseCase. (Runs with pytest)
  2. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /