Skip to content

Commit

Permalink
fix: make it compatible with unittest.TestCase (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt authored Jun 7, 2021
1 parent aae2b43 commit 36e5a49
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 21 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,30 @@ def context(

When using that all pages inside your test are created from the persistent context.

### Using with `unittest.TestCase`

See the following example for using it with `unittest.TestCase`. This has a limitation,
that only a single browser can be specified and no matrix of multiple browsers gets
generated when specifying multiple.

```py
import pytest
import unittest

from playwright.sync_api import Page


class MyTest(unittest.TestCase):
@pytest.fixture(autouse=True)
def setup(self, page: Page):
self.page = page

def test_foobar(self):
self.page.goto("https://microsoft.com")
self.page.click("#foobar")
assert self.page.evaluate("1 + 1") == 2
```

## Debugging

### Use with pdb
Expand Down
15 changes: 13 additions & 2 deletions pytest_playwright/pytest_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import asyncio
import warnings
from asyncio import AbstractEventLoop
from typing import Any, Callable, Dict, Generator, List, Optional

Expand Down Expand Up @@ -183,8 +184,18 @@ def is_chromium(browser_name: str) -> bool:


@pytest.fixture(scope="session")
def browser_name() -> None:
return None
def browser_name(pytestconfig: Any) -> Optional[str]:
# When using unittest.TestCase it won't use pytest_generate_tests
# For that we still try to give the user a slightly less feature-rich experience
browser_names = pytestconfig.getoption("--browser")
if len(browser_names) == 0:
return "chromium"
if len(browser_names) == 1:
return browser_names[0]
warnings.warn(
"When using unittest.TestCase specifying multiple browsers is not supported"
)
return browser_names[0]


@pytest.fixture(scope="session")
Expand Down
81 changes: 62 additions & 19 deletions tests/test_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
# limitations under the License.

import sys
from typing import Any

import pytest


def test_default(testdir: Any) -> None:
def test_default(testdir: pytest.Testdir) -> None:
testdir.makepyfile(
"""
import pytest
Expand All @@ -35,7 +34,7 @@ def test_default(page, browser_name):
result.assert_outcomes(passed=1)


def test_slowmo(testdir: Any) -> None:
def test_slowmo(testdir: pytest.Testdir) -> None:
testdir.makepyfile(
"""
from time import monotonic
Expand All @@ -59,7 +58,7 @@ def test_slowmo(page):
"msedge",
],
)
def test_browser_channel(channel: str, testdir: Any) -> None:
def test_browser_channel(channel: str, testdir: pytest.Testdir) -> None:
if channel == "msedge" and sys.platform == "linux":
pytest.skip("msedge not supported on linux")
testdir.makepyfile(
Expand All @@ -75,7 +74,7 @@ def test_browser_channel(page, browser_name, browser_channel):
result.assert_outcomes(passed=1)


def test_invalid_browser_channel(testdir: Any) -> None:
def test_invalid_browser_channel(testdir: pytest.Testdir) -> None:
testdir.makepyfile(
"""
import pytest
Expand All @@ -89,7 +88,51 @@ def test_browser_channel(page, browser_name, browser_channel):
assert "channel: expected one of " in "\n".join(result.outlines)


def test_multiple_browsers(testdir: Any) -> None:
def test_unittest_class(testdir: pytest.Testdir) -> None:
testdir.makepyfile(
"""
import pytest
import unittest
from playwright.sync_api import Page
class MyTest(unittest.TestCase):
@pytest.fixture(autouse=True)
def setup(self, page: Page):
self.page = page
def test_foobar(self):
assert self.page.evaluate("1 + 1") == 2
"""
)
result = testdir.runpytest("--browser", "chromium")
result.assert_outcomes(passed=1)


def test_unittest_class_multiple_browsers(testdir: pytest.Testdir) -> None:
testdir.makepyfile(
"""
import pytest
import unittest
from playwright.sync_api import Page
class MyTest(unittest.TestCase):
@pytest.fixture(autouse=True)
def setup(self, page: Page):
self.page = page
def test_foobar(self):
assert "Firefox" in self.page.evaluate("navigator.userAgent")
assert self.page.evaluate("1 + 1") == 2
"""
)
result = testdir.runpytest("--browser", "firefox", "--browser", "webkit")
result.assert_outcomes(passed=1)
assert any("multiple browsers is not supported" in line for line in result.outlines)


def test_multiple_browsers(testdir: pytest.Testdir) -> None:
testdir.makepyfile(
"""
def test_multiple_browsers(page):
Expand All @@ -103,7 +146,7 @@ def test_multiple_browsers(page):
result.assert_outcomes(passed=3)


def test_browser_context_args(testdir: Any) -> None:
def test_browser_context_args(testdir: pytest.Testdir) -> None:
testdir.makeconftest(
"""
import pytest
Expand All @@ -123,7 +166,7 @@ def test_browser_context_args(page):
result.assert_outcomes(passed=1)


def test_chromium(testdir: Any) -> None:
def test_chromium(testdir: pytest.Testdir) -> None:
testdir.makepyfile(
"""
def test_is_chromium(page, browser_name, is_chromium, is_firefox, is_webkit):
Expand All @@ -137,7 +180,7 @@ def test_is_chromium(page, browser_name, is_chromium, is_firefox, is_webkit):
result.assert_outcomes(passed=1)


def test_firefox(testdir: Any) -> None:
def test_firefox(testdir: pytest.Testdir) -> None:
testdir.makepyfile(
"""
def test_is_firefox(page, browser_name, is_chromium, is_firefox, is_webkit):
Expand All @@ -151,7 +194,7 @@ def test_is_firefox(page, browser_name, is_chromium, is_firefox, is_webkit):
result.assert_outcomes(passed=1)


def test_webkit(testdir: Any) -> None:
def test_webkit(testdir: pytest.Testdir) -> None:
testdir.makepyfile(
"""
def test_is_webkit(page, browser_name, is_chromium, is_firefox, is_webkit):
Expand All @@ -165,7 +208,7 @@ def test_is_webkit(page, browser_name, is_chromium, is_firefox, is_webkit):
result.assert_outcomes(passed=1)


def test_goto(testdir: Any) -> None:
def test_goto(testdir: pytest.Testdir) -> None:
testdir.makepyfile(
"""
def test_base_url(page, base_url):
Expand All @@ -180,7 +223,7 @@ def test_base_url(page, base_url):
result.assert_outcomes(passed=1)


def test_skip_browsers(testdir: Any) -> None:
def test_skip_browsers(testdir: pytest.Testdir) -> None:
testdir.makepyfile(
"""
import pytest
Expand All @@ -196,7 +239,7 @@ def test_base_url(page, browser_name):
result.assert_outcomes(passed=2, skipped=1)


def test_only_browser(testdir: Any) -> None:
def test_only_browser(testdir: pytest.Testdir) -> None:
testdir.makepyfile(
"""
import pytest
Expand All @@ -212,7 +255,7 @@ def test_base_url(page, browser_name):
result.assert_outcomes(passed=1, skipped=2)


def test_parameterization(testdir: Any) -> None:
def test_parameterization(testdir: pytest.Testdir) -> None:
testdir.makepyfile(
"""
def test_all_browsers(page):
Expand All @@ -235,7 +278,7 @@ def test_without_browser():
assert "test_without_browser PASSED" in "\n".join(result.outlines)


def test_headed(testdir: Any) -> None:
def test_headed(testdir: pytest.Testdir) -> None:
testdir.makepyfile(
"""
def test_base_url(page, browser_name):
Expand All @@ -247,7 +290,7 @@ def test_base_url(page, browser_name):
result.assert_outcomes(passed=1)


def test_invalid_browser_name(testdir: Any) -> None:
def test_invalid_browser_name(testdir: pytest.Testdir) -> None:
testdir.makepyfile(
"""
def test_base_url(page):
Expand All @@ -259,7 +302,7 @@ def test_base_url(page):
assert "'test123' is not allowed" in "\n".join(result.outlines)


def test_django(testdir: Any) -> None:
def test_django(testdir: pytest.Testdir) -> None:
testdir.makepyfile(
"""
from django.test import TestCase
Expand All @@ -273,7 +316,7 @@ def test_one(self):
result.assert_outcomes(passed=1)


def test_device_emulation(testdir: Any) -> None:
def test_device_emulation(testdir: pytest.Testdir) -> None:
testdir.makeconftest(
"""
import pytest
Expand All @@ -294,7 +337,7 @@ def test_browser_context_args(page):
result.assert_outcomes(passed=1)


def test_launch_persistent_context(testdir: Any) -> None:
def test_launch_persistent_context(testdir: pytest.Testdir) -> None:
testdir.makeconftest(
"""
import pytest
Expand Down

0 comments on commit 36e5a49

Please sign in to comment.