Python Skipping Unittests
I have Python 2.7 selenium unit tests that need to run in a particular order and if one test fails inside a class, there is no point in running the remainging tests in that class. I know this is bad and the tests should run independently.
In the code below, I'm trying to use test_a to set bar=False so that test_f gets skipped. This is not working for me and I was trying to figure out why.
from unittest import TestCase, main as unittest_main, skipIf
class TestSimpleFoo(TestCase):
foo = 'bar'
bar = True
def setUp(self):
pass
def test_a(self):
self.assertEqual(self.__class__.foo, 'bar')
self.__class__.foo = 'can'
# can i skip test_f ???
self.__class__.bar = False
@skipIf(bar == False, "skipping")
def test_f(self):
self.assertEqual(self.__class__.foo, 'can')
if __name__ == '__main__':
unittest_main()
python python-unittest
add a comment |
I have Python 2.7 selenium unit tests that need to run in a particular order and if one test fails inside a class, there is no point in running the remainging tests in that class. I know this is bad and the tests should run independently.
In the code below, I'm trying to use test_a to set bar=False so that test_f gets skipped. This is not working for me and I was trying to figure out why.
from unittest import TestCase, main as unittest_main, skipIf
class TestSimpleFoo(TestCase):
foo = 'bar'
bar = True
def setUp(self):
pass
def test_a(self):
self.assertEqual(self.__class__.foo, 'bar')
self.__class__.foo = 'can'
# can i skip test_f ???
self.__class__.bar = False
@skipIf(bar == False, "skipping")
def test_f(self):
self.assertEqual(self.__class__.foo, 'can')
if __name__ == '__main__':
unittest_main()
python python-unittest
I would suggest raising an error inside the test that you think is being skipped just to be sure that it is being skipped.
– JacobIRR
Nov 14 '18 at 23:12
You should perform the skip check inside the test case method, and then raiseunittest.SkipTest(reason)
.
– wowserx
Nov 14 '18 at 23:51
In this case, I'll go inside the test and check for my condition and useself.fail('Reason')
– 46boomerang
Nov 15 '18 at 4:09
add a comment |
I have Python 2.7 selenium unit tests that need to run in a particular order and if one test fails inside a class, there is no point in running the remainging tests in that class. I know this is bad and the tests should run independently.
In the code below, I'm trying to use test_a to set bar=False so that test_f gets skipped. This is not working for me and I was trying to figure out why.
from unittest import TestCase, main as unittest_main, skipIf
class TestSimpleFoo(TestCase):
foo = 'bar'
bar = True
def setUp(self):
pass
def test_a(self):
self.assertEqual(self.__class__.foo, 'bar')
self.__class__.foo = 'can'
# can i skip test_f ???
self.__class__.bar = False
@skipIf(bar == False, "skipping")
def test_f(self):
self.assertEqual(self.__class__.foo, 'can')
if __name__ == '__main__':
unittest_main()
python python-unittest
I have Python 2.7 selenium unit tests that need to run in a particular order and if one test fails inside a class, there is no point in running the remainging tests in that class. I know this is bad and the tests should run independently.
In the code below, I'm trying to use test_a to set bar=False so that test_f gets skipped. This is not working for me and I was trying to figure out why.
from unittest import TestCase, main as unittest_main, skipIf
class TestSimpleFoo(TestCase):
foo = 'bar'
bar = True
def setUp(self):
pass
def test_a(self):
self.assertEqual(self.__class__.foo, 'bar')
self.__class__.foo = 'can'
# can i skip test_f ???
self.__class__.bar = False
@skipIf(bar == False, "skipping")
def test_f(self):
self.assertEqual(self.__class__.foo, 'can')
if __name__ == '__main__':
unittest_main()
python python-unittest
python python-unittest
asked Nov 14 '18 at 23:07
46boomerang46boomerang
33
33
I would suggest raising an error inside the test that you think is being skipped just to be sure that it is being skipped.
– JacobIRR
Nov 14 '18 at 23:12
You should perform the skip check inside the test case method, and then raiseunittest.SkipTest(reason)
.
– wowserx
Nov 14 '18 at 23:51
In this case, I'll go inside the test and check for my condition and useself.fail('Reason')
– 46boomerang
Nov 15 '18 at 4:09
add a comment |
I would suggest raising an error inside the test that you think is being skipped just to be sure that it is being skipped.
– JacobIRR
Nov 14 '18 at 23:12
You should perform the skip check inside the test case method, and then raiseunittest.SkipTest(reason)
.
– wowserx
Nov 14 '18 at 23:51
In this case, I'll go inside the test and check for my condition and useself.fail('Reason')
– 46boomerang
Nov 15 '18 at 4:09
I would suggest raising an error inside the test that you think is being skipped just to be sure that it is being skipped.
– JacobIRR
Nov 14 '18 at 23:12
I would suggest raising an error inside the test that you think is being skipped just to be sure that it is being skipped.
– JacobIRR
Nov 14 '18 at 23:12
You should perform the skip check inside the test case method, and then raise
unittest.SkipTest(reason)
.– wowserx
Nov 14 '18 at 23:51
You should perform the skip check inside the test case method, and then raise
unittest.SkipTest(reason)
.– wowserx
Nov 14 '18 at 23:51
In this case, I'll go inside the test and check for my condition and use
self.fail('Reason')
– 46boomerang
Nov 15 '18 at 4:09
In this case, I'll go inside the test and check for my condition and use
self.fail('Reason')
– 46boomerang
Nov 15 '18 at 4:09
add a comment |
1 Answer
1
active
oldest
votes
You mean, this doesn't work when test_a
is run before test_f
?
This is normal behavior, as the condition is tested when the decorator is constructed, which is before test_a
is executed (when the unit test classes are registered).
It feels like you want to deactivate tests if previous tests are broken, but this is not the proper way of doing so.
add a comment |
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%2f53310098%2fpython-skipping-unittests%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
You mean, this doesn't work when test_a
is run before test_f
?
This is normal behavior, as the condition is tested when the decorator is constructed, which is before test_a
is executed (when the unit test classes are registered).
It feels like you want to deactivate tests if previous tests are broken, but this is not the proper way of doing so.
add a comment |
You mean, this doesn't work when test_a
is run before test_f
?
This is normal behavior, as the condition is tested when the decorator is constructed, which is before test_a
is executed (when the unit test classes are registered).
It feels like you want to deactivate tests if previous tests are broken, but this is not the proper way of doing so.
add a comment |
You mean, this doesn't work when test_a
is run before test_f
?
This is normal behavior, as the condition is tested when the decorator is constructed, which is before test_a
is executed (when the unit test classes are registered).
It feels like you want to deactivate tests if previous tests are broken, but this is not the proper way of doing so.
You mean, this doesn't work when test_a
is run before test_f
?
This is normal behavior, as the condition is tested when the decorator is constructed, which is before test_a
is executed (when the unit test classes are registered).
It feels like you want to deactivate tests if previous tests are broken, but this is not the proper way of doing so.
answered Nov 14 '18 at 23:12
Matthieu BrucherMatthieu Brucher
15.9k32141
15.9k32141
add a comment |
add a comment |
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%2f53310098%2fpython-skipping-unittests%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
I would suggest raising an error inside the test that you think is being skipped just to be sure that it is being skipped.
– JacobIRR
Nov 14 '18 at 23:12
You should perform the skip check inside the test case method, and then raise
unittest.SkipTest(reason)
.– wowserx
Nov 14 '18 at 23:51
In this case, I'll go inside the test and check for my condition and use
self.fail('Reason')
– 46boomerang
Nov 15 '18 at 4:09