Skip to content

Commit 23a7d77

Browse files
committed
[py] Lay groundwork for running large tests with python
Turns out we need a custom runner. Implementing this as a macro that generates the runner for us.
1 parent 5a20d44 commit 23a7d77

File tree

10 files changed

+224
-13
lines changed

10 files changed

+224
-13
lines changed

common/src/web/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ filegroup(
88
"//dotnet/test/common:__pkg__",
99
"//java/client/test/org/openqa/selenium/environment:__pkg__",
1010
"//javascript/node/selenium-webdriver:__pkg__",
11+
"//py:__pkg__",
1112
],
1213
)

java/server/src/org/openqa/selenium/grid/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ java_binary(
3737
],
3838
visibility = [
3939
"//:__pkg__",
40+
"//py:__pkg__",
4041
],
4142
)
4243

py/BUILD.bazel

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
2+
load("//py:defs.bzl", "pytest_test")
3+
load("//:copy_file.bzl", "copy_file")
24

3-
genrule(
5+
copy_file(
46
name = "get-attribute",
5-
srcs = ["//javascript/webdriver/atoms:get-attribute.js"],
6-
outs = ["selenium/webdriver/remote/getAttribute.js"],
7-
cmd = "cp $< $@",
7+
src = "//javascript/webdriver/atoms:get-attribute.js",
8+
out = "selenium/webdriver/remote/getAttribute.js",
89
)
910

10-
genrule(
11+
copy_file(
1112
name = "is-displayed",
12-
srcs = ["//javascript/atoms/fragments:is-displayed.js"],
13-
outs = ["selenium/webdriver/remote/isDisplayed.js"],
14-
cmd = "cp $< $@",
13+
src = "//javascript/atoms/fragments:is-displayed.js",
14+
out = "selenium/webdriver/remote/isDisplayed.js",
15+
)
16+
17+
copy_file(
18+
name = "firefox-driver-prefs",
19+
src = "//third_party/js/selenium:webdriver_json",
20+
out = "selenium/webdriver/firefox/webdriver_prefs.json",
1521
)
1622

1723
py_library(
1824
name = "main",
1925
srcs = glob(["selenium/**/*.py"]),
2026
data = [
27+
":firefox-driver-prefs",
2128
":get-attribute",
2229
":is-displayed",
2330
],
@@ -28,18 +35,85 @@ py_library(
2835
visibility = ["//visibility:public"],
2936
)
3037

31-
py_test(
38+
py_library(
39+
name = "init-tree",
40+
testonly = True,
41+
srcs = [
42+
"conftest.py",
43+
"test/__init__.py",
44+
"test/selenium/__init__.py",
45+
"test/selenium/webdriver/__init__.py",
46+
"test/selenium/webdriver/chrome/__init__.py",
47+
"test/selenium/webdriver/common/__init__.py",
48+
"test/selenium/webdriver/common/conftest.py",
49+
"test/selenium/webdriver/common/network.py",
50+
"test/selenium/webdriver/common/webserver.py",
51+
"test/selenium/webdriver/firefox/__init__.py",
52+
"test/selenium/webdriver/firefox/conftest.py",
53+
"test/selenium/webdriver/marionette/__init__.py",
54+
"test/selenium/webdriver/marionette/conftest.py",
55+
"test/selenium/webdriver/safari/conftest.py",
56+
"test/selenium/webdriver/support/__init__.py",
57+
"test/selenium/webdriver/support/conftest.py",
58+
],
59+
deps = [
60+
":webserver",
61+
],
62+
data = [
63+
"setup.cfg",
64+
],
65+
imports = ["."],
66+
)
67+
68+
pytest_test(
3269
name = "unit",
3370
size = "small",
71+
python_version = "PY2",
72+
args = [
73+
"-n=auto",
74+
],
3475
srcs = glob([
3576
"test/unit/**/*.py",
36-
]) + [ "test/runner/run_pytest.py" ],
37-
main = "test/runner/run_pytest.py",
77+
]),
3878
deps = [
79+
":init-tree",
3980
":main",
4081
"//third_party/py:pytest",
4182
],
42-
legacy_create_init = False,
83+
)
84+
85+
py_library(
86+
name = "webserver",
87+
testonly = True,
88+
srcs = [
89+
"test/selenium/webdriver/common/network.py",
90+
"test/selenium/webdriver/common/webserver.py",
91+
],
92+
data = [
93+
"//common/src/web",
94+
"//java/server/src/org/openqa/selenium/grid:selenium_server_deploy.jar",
95+
],
96+
deps = [],
97+
)
98+
99+
pytest_test(
100+
name = "large-tests",
101+
size = "large",
102+
srcs = glob([
103+
"test/selenium/webdriver/common/**/*.py",
104+
"test/selenium/webdriver/support/**/*.py",
105+
]),
106+
args = ["--driver=Firefox"],
107+
python_version = "PY2",
108+
tags = [
109+
"no-sandbox",
110+
],
111+
deps = [
112+
":init-tree",
113+
":main",
114+
":webserver",
115+
"//third_party/py:pytest",
116+
]
43117
)
44118

45119
py_binary(

py/defs.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
load("//py:import.bzl", _py_import = "py_import")
2+
load("//py/private:pytest.bzl", _pytest_test = "pytest_test")
23

34
py_import = _py_import
5+
pytest_test = _pytest_test

py/private/BUILD.bazel

Whitespace-only changes.

py/private/pytest.bzl

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
load("@rules_python//python:defs.bzl", "PyInfo", "PyRuntimeInfo", "py_test")
2+
3+
def _stringify(paths):
4+
return "[%s]" % (", ".join(["\"%s\"" % path for path in paths]))
5+
6+
def _pytest_runner_impl(ctx):
7+
if len(ctx.attr.srcs) == 0:
8+
fail("No test files specified.")
9+
10+
runner = ctx.actions.declare_file(ctx.attr.name)
11+
ctx.actions.write(
12+
runner,
13+
"""
14+
if __name__ == "__main__":
15+
import sys
16+
import pytest
17+
18+
args = sys.argv[1:] + ["-ra", "-s", "--instafail"] + %s + %s
19+
20+
sys.exit(pytest.main(args))""" % (_stringify(ctx.attr.args), _stringify([src.path for src in ctx.files.srcs])),
21+
is_executable = True)
22+
23+
return [
24+
DefaultInfo(
25+
files = depset([runner]),
26+
executable = runner,
27+
),
28+
]
29+
30+
_pytest_runner = rule(
31+
_pytest_runner_impl,
32+
attrs = {
33+
"srcs": attr.label_list(
34+
allow_files = [".py"],
35+
),
36+
"deps": attr.label_list(
37+
providers = [
38+
PyInfo,
39+
],
40+
),
41+
"args": attr.string_list(
42+
default = [],
43+
),
44+
"python_version": attr.string(
45+
values = ["PY2", "PY3"],
46+
default = "PY3",
47+
),
48+
},
49+
toolchains = [
50+
"@rules_python//python:toolchain_type",
51+
]
52+
)
53+
54+
def pytest_test(name, srcs, deps = None, args = None, python_version = None, **kwargs):
55+
runner_target = "%s-runner.py" % name
56+
57+
_pytest_runner(
58+
name = runner_target,
59+
testonly = True,
60+
srcs = srcs,
61+
deps = deps,
62+
args = args,
63+
python_version = python_version,
64+
)
65+
66+
py_test(
67+
name = name,
68+
python_version = python_version,
69+
srcs = srcs + [runner_target],
70+
deps = deps,
71+
main = runner_target,
72+
legacy_create_init = False,
73+
imports = ["."],
74+
**kwargs,
75+
)
76+

py/test/runner/run_pytest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
with open("pytest.ini", "w") as ini_file:
44
ini_file.write("[pytest]\n")
55
ini_file.write("addopts = -r=a\n")
6-
ini_file.write("rootdir = py")
6+
ini_file.write("rootdir = py\n")
77
ini_file.write("python_files = test_*.py *_tests.py\n")
88

99
raise SystemExit(pytest.main())

third_party/js/selenium/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ copy_file(
2020
"//dotnet/src/webdriver:__pkg__",
2121
"//java/client/src/org/openqa/selenium/firefox:__pkg__",
2222
"//java/client/test/org/openqa/selenium/environment:__pkg__",
23+
"//py:__pkg__",
2324
],
2425
)

third_party/py/BUILD.bazel

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
load("//py:defs.bzl", "py_import")
22

3+
py_import(
4+
name = "apipkg",
5+
wheel = "apipkg-1.4-py2.py3-none-any.whl",
6+
)
7+
8+
py_import(
9+
name = "execnet",
10+
wheel = "execnet-1.4.1-py2.py3-none-any.whl",
11+
deps = [
12+
":apipkg",
13+
],
14+
)
15+
16+
py_import(
17+
name = "mock",
18+
wheel = "mock-2.0.0-py2.py3-none-any.whl",
19+
deps = [
20+
":pbr",
21+
":six",
22+
]
23+
)
24+
25+
py_import(
26+
name = "pbr",
27+
wheel = "pbr-2.0.0-py2.py3-none-any.whl",
28+
)
29+
330
py_import(
431
name = "py",
532
wheel = "py-1.4.32-py2.py3-none-any.whl",
@@ -10,7 +37,10 @@ py_import(
1037
wheel = "pytest-3.0.3-py2.py3-none-any.whl",
1138
deps = [
1239
":py",
40+
":pytest-instafail",
1341
":pytest-mock",
42+
# ":pytest-timeout",
43+
":pytest-xdist",
1444
],
1545
visibility = [
1646
"//visibility:public",
@@ -20,6 +50,32 @@ py_import(
2050
py_import(
2151
name = "pytest-mock",
2252
wheel = "pytest_mock-1.5.0-py2.py3-none-any.whl",
53+
deps = [
54+
"mock",
55+
],
56+
)
57+
58+
py_import(
59+
name = "pytest-instafail",
60+
wheel = "pytest-instafail-0.3.0.tar.gz",
61+
)
62+
63+
py_import(
64+
name = "pytest-timeout",
65+
wheel = "pytest_timeout-1.2.0-py2.py3-none-any.whl",
66+
)
67+
68+
py_import(
69+
name = "pytest-xdist",
70+
wheel = "pytest-xdist-1.15.0.tar.gz",
71+
deps = [
72+
":execnet",
73+
],
74+
)
75+
76+
py_import(
77+
name = "six",
78+
wheel = "six-1.11.0.tar.gz",
2379
)
2480

2581
py_import(
Binary file not shown.

0 commit comments

Comments
 (0)