How to use pytest to pass a value in your test using command line args?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have the following as conftest.py -->
def pytest_addoption(parser):
parser.addoption("--browser")
parser.addoption("--osType", help="Type of operating system")
parser.addoption("--hostURL", action="store", help="prod, stage, or dev")
@pytest.fixture(scope="session")
def browser(request):
return request.config.getoption("--browser")
@pytest.fixture(scope="session")
def osType(request):
return request.config.getoption("--osType")
@pytest.fixture(autouse=True)
def hostURL(request):
return request.config.getoption("--hostURL")
I would like to use the --hostURL flag to pass in value such as prod, stage or dev.
Here's how my test_TheMainTest.py looks -->
import unitest
import pytest
class CheckStatusCodeTest(unittest.TestCase, LoginPage, CustomSeleniumDriver):
def test_CheckStatusCodeOfPages(self, hostURL):
self.login(hostURL)
When I run the above test using pytest -q -s --hostURL prod I get the following error -->
TypeError: test_CheckStatusCodeOfCRPages() missing 1 required positional argument: 'hostURL'
python selenium pytest
add a comment |
I have the following as conftest.py -->
def pytest_addoption(parser):
parser.addoption("--browser")
parser.addoption("--osType", help="Type of operating system")
parser.addoption("--hostURL", action="store", help="prod, stage, or dev")
@pytest.fixture(scope="session")
def browser(request):
return request.config.getoption("--browser")
@pytest.fixture(scope="session")
def osType(request):
return request.config.getoption("--osType")
@pytest.fixture(autouse=True)
def hostURL(request):
return request.config.getoption("--hostURL")
I would like to use the --hostURL flag to pass in value such as prod, stage or dev.
Here's how my test_TheMainTest.py looks -->
import unitest
import pytest
class CheckStatusCodeTest(unittest.TestCase, LoginPage, CustomSeleniumDriver):
def test_CheckStatusCodeOfPages(self, hostURL):
self.login(hostURL)
When I run the above test using pytest -q -s --hostURL prod I get the following error -->
TypeError: test_CheckStatusCodeOfCRPages() missing 1 required positional argument: 'hostURL'
python selenium pytest
add a comment |
I have the following as conftest.py -->
def pytest_addoption(parser):
parser.addoption("--browser")
parser.addoption("--osType", help="Type of operating system")
parser.addoption("--hostURL", action="store", help="prod, stage, or dev")
@pytest.fixture(scope="session")
def browser(request):
return request.config.getoption("--browser")
@pytest.fixture(scope="session")
def osType(request):
return request.config.getoption("--osType")
@pytest.fixture(autouse=True)
def hostURL(request):
return request.config.getoption("--hostURL")
I would like to use the --hostURL flag to pass in value such as prod, stage or dev.
Here's how my test_TheMainTest.py looks -->
import unitest
import pytest
class CheckStatusCodeTest(unittest.TestCase, LoginPage, CustomSeleniumDriver):
def test_CheckStatusCodeOfPages(self, hostURL):
self.login(hostURL)
When I run the above test using pytest -q -s --hostURL prod I get the following error -->
TypeError: test_CheckStatusCodeOfCRPages() missing 1 required positional argument: 'hostURL'
python selenium pytest
I have the following as conftest.py -->
def pytest_addoption(parser):
parser.addoption("--browser")
parser.addoption("--osType", help="Type of operating system")
parser.addoption("--hostURL", action="store", help="prod, stage, or dev")
@pytest.fixture(scope="session")
def browser(request):
return request.config.getoption("--browser")
@pytest.fixture(scope="session")
def osType(request):
return request.config.getoption("--osType")
@pytest.fixture(autouse=True)
def hostURL(request):
return request.config.getoption("--hostURL")
I would like to use the --hostURL flag to pass in value such as prod, stage or dev.
Here's how my test_TheMainTest.py looks -->
import unitest
import pytest
class CheckStatusCodeTest(unittest.TestCase, LoginPage, CustomSeleniumDriver):
def test_CheckStatusCodeOfPages(self, hostURL):
self.login(hostURL)
When I run the above test using pytest -q -s --hostURL prod I get the following error -->
TypeError: test_CheckStatusCodeOfCRPages() missing 1 required positional argument: 'hostURL'
python selenium pytest
python selenium pytest
edited Jan 4 at 16:29
Ash
asked Jan 3 at 21:36
AshAsh
45128
45128
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Quoting the docs:
Note
unittest.TestCasemethods cannot directly receive fixture arguments as implementing that is likely to inflict on the ability to run generalunittest.TestCasetest suites.
However, you can still pass regular fixture values to unittest-style tests using autouse fixtures:
class CheckStatusCodeTest(unittest.TestCase):
@pytest.fixture(autouse=True)
def _pass_fixture_value(self, hostURL):
self._hostURL = hostURL
def test_CheckStatusCodeOfPages(self):
assert self._hostURL
You can also check out this answer of mine tackling the same issue for more examples.
Another possibility is to implement an autouse fixture that modifies the test class explicitly. This is useful if you have lots of test classes that should have the identical setup:
@pytest.fixture(scope="class")
def set_fixture_value_to_class(request, hostURL):
request.cls._hostURL = hostURL
@pytest.mark.usefixtures('set_fixture_value_to_class')
class CheckStatusCodeTest(unittest.TestCase):
def test_CheckStatusCodeOfPages(self):
assert self._hostURL
@pytest.mark.usefixtures('set_fixture_value_to_class')
class AnotherTest(unittest.TestCase):
def test_spam(self):
assert self._hostURL
In this case, no need to copy the same fixture to each test class. Just mark all relevant test classes and you're good to go.
Thank you so much :)
– Ash
Jan 3 at 22:28
Glad I could help!
– hoefling
Jan 3 at 22:30
quick question.. I'm trying to access that hostURL as a command line args. Will your solution help me do that?
– Ash
Jan 4 at 15:58
Shouldn't be an issue, just includerequestin the fixture parameters and get the value viarequest.config.getoption("--hostURL"), like you're already doing it in your original code. Or do you have another problem with that?
– hoefling
Jan 4 at 16:02
so in the conftest.py I've added this -->@pytest.fixture(autouse=True) def heyheyhey(request): return request.config.getoption("--hostURL")and in my test_MainTest.py I have -->class CheckStatusCodeTest(unittest.TestCase, CheckStatusCode, LoginPage): def test_CheckStatusCodeOfCRPages(self, heyheyhey): self.login(heyheyhey)I'm still getting themissing 1 required positional argument: 'heyheyhey'error. @hoefling
– Ash
Jan 4 at 16:12
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030132%2fhow-to-use-pytest-to-pass-a-value-in-your-test-using-command-line-args%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Quoting the docs:
Note
unittest.TestCasemethods cannot directly receive fixture arguments as implementing that is likely to inflict on the ability to run generalunittest.TestCasetest suites.
However, you can still pass regular fixture values to unittest-style tests using autouse fixtures:
class CheckStatusCodeTest(unittest.TestCase):
@pytest.fixture(autouse=True)
def _pass_fixture_value(self, hostURL):
self._hostURL = hostURL
def test_CheckStatusCodeOfPages(self):
assert self._hostURL
You can also check out this answer of mine tackling the same issue for more examples.
Another possibility is to implement an autouse fixture that modifies the test class explicitly. This is useful if you have lots of test classes that should have the identical setup:
@pytest.fixture(scope="class")
def set_fixture_value_to_class(request, hostURL):
request.cls._hostURL = hostURL
@pytest.mark.usefixtures('set_fixture_value_to_class')
class CheckStatusCodeTest(unittest.TestCase):
def test_CheckStatusCodeOfPages(self):
assert self._hostURL
@pytest.mark.usefixtures('set_fixture_value_to_class')
class AnotherTest(unittest.TestCase):
def test_spam(self):
assert self._hostURL
In this case, no need to copy the same fixture to each test class. Just mark all relevant test classes and you're good to go.
Thank you so much :)
– Ash
Jan 3 at 22:28
Glad I could help!
– hoefling
Jan 3 at 22:30
quick question.. I'm trying to access that hostURL as a command line args. Will your solution help me do that?
– Ash
Jan 4 at 15:58
Shouldn't be an issue, just includerequestin the fixture parameters and get the value viarequest.config.getoption("--hostURL"), like you're already doing it in your original code. Or do you have another problem with that?
– hoefling
Jan 4 at 16:02
so in the conftest.py I've added this -->@pytest.fixture(autouse=True) def heyheyhey(request): return request.config.getoption("--hostURL")and in my test_MainTest.py I have -->class CheckStatusCodeTest(unittest.TestCase, CheckStatusCode, LoginPage): def test_CheckStatusCodeOfCRPages(self, heyheyhey): self.login(heyheyhey)I'm still getting themissing 1 required positional argument: 'heyheyhey'error. @hoefling
– Ash
Jan 4 at 16:12
|
show 3 more comments
Quoting the docs:
Note
unittest.TestCasemethods cannot directly receive fixture arguments as implementing that is likely to inflict on the ability to run generalunittest.TestCasetest suites.
However, you can still pass regular fixture values to unittest-style tests using autouse fixtures:
class CheckStatusCodeTest(unittest.TestCase):
@pytest.fixture(autouse=True)
def _pass_fixture_value(self, hostURL):
self._hostURL = hostURL
def test_CheckStatusCodeOfPages(self):
assert self._hostURL
You can also check out this answer of mine tackling the same issue for more examples.
Another possibility is to implement an autouse fixture that modifies the test class explicitly. This is useful if you have lots of test classes that should have the identical setup:
@pytest.fixture(scope="class")
def set_fixture_value_to_class(request, hostURL):
request.cls._hostURL = hostURL
@pytest.mark.usefixtures('set_fixture_value_to_class')
class CheckStatusCodeTest(unittest.TestCase):
def test_CheckStatusCodeOfPages(self):
assert self._hostURL
@pytest.mark.usefixtures('set_fixture_value_to_class')
class AnotherTest(unittest.TestCase):
def test_spam(self):
assert self._hostURL
In this case, no need to copy the same fixture to each test class. Just mark all relevant test classes and you're good to go.
Thank you so much :)
– Ash
Jan 3 at 22:28
Glad I could help!
– hoefling
Jan 3 at 22:30
quick question.. I'm trying to access that hostURL as a command line args. Will your solution help me do that?
– Ash
Jan 4 at 15:58
Shouldn't be an issue, just includerequestin the fixture parameters and get the value viarequest.config.getoption("--hostURL"), like you're already doing it in your original code. Or do you have another problem with that?
– hoefling
Jan 4 at 16:02
so in the conftest.py I've added this -->@pytest.fixture(autouse=True) def heyheyhey(request): return request.config.getoption("--hostURL")and in my test_MainTest.py I have -->class CheckStatusCodeTest(unittest.TestCase, CheckStatusCode, LoginPage): def test_CheckStatusCodeOfCRPages(self, heyheyhey): self.login(heyheyhey)I'm still getting themissing 1 required positional argument: 'heyheyhey'error. @hoefling
– Ash
Jan 4 at 16:12
|
show 3 more comments
Quoting the docs:
Note
unittest.TestCasemethods cannot directly receive fixture arguments as implementing that is likely to inflict on the ability to run generalunittest.TestCasetest suites.
However, you can still pass regular fixture values to unittest-style tests using autouse fixtures:
class CheckStatusCodeTest(unittest.TestCase):
@pytest.fixture(autouse=True)
def _pass_fixture_value(self, hostURL):
self._hostURL = hostURL
def test_CheckStatusCodeOfPages(self):
assert self._hostURL
You can also check out this answer of mine tackling the same issue for more examples.
Another possibility is to implement an autouse fixture that modifies the test class explicitly. This is useful if you have lots of test classes that should have the identical setup:
@pytest.fixture(scope="class")
def set_fixture_value_to_class(request, hostURL):
request.cls._hostURL = hostURL
@pytest.mark.usefixtures('set_fixture_value_to_class')
class CheckStatusCodeTest(unittest.TestCase):
def test_CheckStatusCodeOfPages(self):
assert self._hostURL
@pytest.mark.usefixtures('set_fixture_value_to_class')
class AnotherTest(unittest.TestCase):
def test_spam(self):
assert self._hostURL
In this case, no need to copy the same fixture to each test class. Just mark all relevant test classes and you're good to go.
Quoting the docs:
Note
unittest.TestCasemethods cannot directly receive fixture arguments as implementing that is likely to inflict on the ability to run generalunittest.TestCasetest suites.
However, you can still pass regular fixture values to unittest-style tests using autouse fixtures:
class CheckStatusCodeTest(unittest.TestCase):
@pytest.fixture(autouse=True)
def _pass_fixture_value(self, hostURL):
self._hostURL = hostURL
def test_CheckStatusCodeOfPages(self):
assert self._hostURL
You can also check out this answer of mine tackling the same issue for more examples.
Another possibility is to implement an autouse fixture that modifies the test class explicitly. This is useful if you have lots of test classes that should have the identical setup:
@pytest.fixture(scope="class")
def set_fixture_value_to_class(request, hostURL):
request.cls._hostURL = hostURL
@pytest.mark.usefixtures('set_fixture_value_to_class')
class CheckStatusCodeTest(unittest.TestCase):
def test_CheckStatusCodeOfPages(self):
assert self._hostURL
@pytest.mark.usefixtures('set_fixture_value_to_class')
class AnotherTest(unittest.TestCase):
def test_spam(self):
assert self._hostURL
In this case, no need to copy the same fixture to each test class. Just mark all relevant test classes and you're good to go.
answered Jan 3 at 22:24
hoeflinghoefling
13.7k43667
13.7k43667
Thank you so much :)
– Ash
Jan 3 at 22:28
Glad I could help!
– hoefling
Jan 3 at 22:30
quick question.. I'm trying to access that hostURL as a command line args. Will your solution help me do that?
– Ash
Jan 4 at 15:58
Shouldn't be an issue, just includerequestin the fixture parameters and get the value viarequest.config.getoption("--hostURL"), like you're already doing it in your original code. Or do you have another problem with that?
– hoefling
Jan 4 at 16:02
so in the conftest.py I've added this -->@pytest.fixture(autouse=True) def heyheyhey(request): return request.config.getoption("--hostURL")and in my test_MainTest.py I have -->class CheckStatusCodeTest(unittest.TestCase, CheckStatusCode, LoginPage): def test_CheckStatusCodeOfCRPages(self, heyheyhey): self.login(heyheyhey)I'm still getting themissing 1 required positional argument: 'heyheyhey'error. @hoefling
– Ash
Jan 4 at 16:12
|
show 3 more comments
Thank you so much :)
– Ash
Jan 3 at 22:28
Glad I could help!
– hoefling
Jan 3 at 22:30
quick question.. I'm trying to access that hostURL as a command line args. Will your solution help me do that?
– Ash
Jan 4 at 15:58
Shouldn't be an issue, just includerequestin the fixture parameters and get the value viarequest.config.getoption("--hostURL"), like you're already doing it in your original code. Or do you have another problem with that?
– hoefling
Jan 4 at 16:02
so in the conftest.py I've added this -->@pytest.fixture(autouse=True) def heyheyhey(request): return request.config.getoption("--hostURL")and in my test_MainTest.py I have -->class CheckStatusCodeTest(unittest.TestCase, CheckStatusCode, LoginPage): def test_CheckStatusCodeOfCRPages(self, heyheyhey): self.login(heyheyhey)I'm still getting themissing 1 required positional argument: 'heyheyhey'error. @hoefling
– Ash
Jan 4 at 16:12
Thank you so much :)
– Ash
Jan 3 at 22:28
Thank you so much :)
– Ash
Jan 3 at 22:28
Glad I could help!
– hoefling
Jan 3 at 22:30
Glad I could help!
– hoefling
Jan 3 at 22:30
quick question.. I'm trying to access that hostURL as a command line args. Will your solution help me do that?
– Ash
Jan 4 at 15:58
quick question.. I'm trying to access that hostURL as a command line args. Will your solution help me do that?
– Ash
Jan 4 at 15:58
Shouldn't be an issue, just include
request in the fixture parameters and get the value via request.config.getoption("--hostURL"), like you're already doing it in your original code. Or do you have another problem with that?– hoefling
Jan 4 at 16:02
Shouldn't be an issue, just include
request in the fixture parameters and get the value via request.config.getoption("--hostURL"), like you're already doing it in your original code. Or do you have another problem with that?– hoefling
Jan 4 at 16:02
so in the conftest.py I've added this -->
@pytest.fixture(autouse=True) def heyheyhey(request): return request.config.getoption("--hostURL") and in my test_MainTest.py I have --> class CheckStatusCodeTest(unittest.TestCase, CheckStatusCode, LoginPage): def test_CheckStatusCodeOfCRPages(self, heyheyhey): self.login(heyheyhey) I'm still getting the missing 1 required positional argument: 'heyheyhey' error. @hoefling– Ash
Jan 4 at 16:12
so in the conftest.py I've added this -->
@pytest.fixture(autouse=True) def heyheyhey(request): return request.config.getoption("--hostURL") and in my test_MainTest.py I have --> class CheckStatusCodeTest(unittest.TestCase, CheckStatusCode, LoginPage): def test_CheckStatusCodeOfCRPages(self, heyheyhey): self.login(heyheyhey) I'm still getting the missing 1 required positional argument: 'heyheyhey' error. @hoefling– Ash
Jan 4 at 16:12
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030132%2fhow-to-use-pytest-to-pass-a-value-in-your-test-using-command-line-args%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown