Unit testing error: “setUpClass error: NameError: global name 'doSomething()' is not defined” [closed]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am trying to execute doSomething() before every test I have. I just want to execute doSomething() one time. However, I am getting the next error message: "setUpClass error: NameError: global name 'doSomething' is not defined"
I have seen several examples of the implementation of setUpClass here in stackoverflow and my code is exactly as in those examples. I have been trying to change a lot of things in my code to make it works with no luck and I feel I am a little bit lost here.
Can anybody help me? Am I missing something?
Here is my code:
class Test_A(unittest.TestCase):
def doSomething(self):
print("class A")
@classmethod
def setUpClass(cls):
doSomething()
def test_1(self):
print("test 1")
def test_2(self):
print("test 2")
I am using python 2.7
python python-2.7
closed as unclear what you're asking by user2357112, petezurich, Billal Begueradj, Rob, Patrick Mevzek Nov 17 '18 at 3:11
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 2 more comments
I am trying to execute doSomething() before every test I have. I just want to execute doSomething() one time. However, I am getting the next error message: "setUpClass error: NameError: global name 'doSomething' is not defined"
I have seen several examples of the implementation of setUpClass here in stackoverflow and my code is exactly as in those examples. I have been trying to change a lot of things in my code to make it works with no luck and I feel I am a little bit lost here.
Can anybody help me? Am I missing something?
Here is my code:
class Test_A(unittest.TestCase):
def doSomething(self):
print("class A")
@classmethod
def setUpClass(cls):
doSomething()
def test_1(self):
print("test 1")
def test_2(self):
print("test 2")
I am using python 2.7
python python-2.7
closed as unclear what you're asking by user2357112, petezurich, Billal Begueradj, Rob, Patrick Mevzek Nov 17 '18 at 3:11
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1
doSomething()
calls a global name, not a method on the class. You can't callcls.doSomething()
because you defineddoSomething
as a regular method, not a class or static method.
– Martijn Pieters♦
Nov 16 '18 at 18:50
1
What instance ofTest_A
do you want to execute thedoSomething
method of?
– user2357112
Nov 16 '18 at 18:50
So, there is no any chance to call doSomething() from setUpClass() ?
– Johnny Chacon
Nov 16 '18 at 19:04
Actually, I have several instances of Test_A. Since it is for Unit Testing I only run the test methods and this is made automatically (it is a little bit complex for me to explain, but Unit Testing works in this way).
– Johnny Chacon
Nov 16 '18 at 19:06
I mean, how I can make setUpClass() call doSomething() ?
– Johnny Chacon
Nov 16 '18 at 19:17
|
show 2 more comments
I am trying to execute doSomething() before every test I have. I just want to execute doSomething() one time. However, I am getting the next error message: "setUpClass error: NameError: global name 'doSomething' is not defined"
I have seen several examples of the implementation of setUpClass here in stackoverflow and my code is exactly as in those examples. I have been trying to change a lot of things in my code to make it works with no luck and I feel I am a little bit lost here.
Can anybody help me? Am I missing something?
Here is my code:
class Test_A(unittest.TestCase):
def doSomething(self):
print("class A")
@classmethod
def setUpClass(cls):
doSomething()
def test_1(self):
print("test 1")
def test_2(self):
print("test 2")
I am using python 2.7
python python-2.7
I am trying to execute doSomething() before every test I have. I just want to execute doSomething() one time. However, I am getting the next error message: "setUpClass error: NameError: global name 'doSomething' is not defined"
I have seen several examples of the implementation of setUpClass here in stackoverflow and my code is exactly as in those examples. I have been trying to change a lot of things in my code to make it works with no luck and I feel I am a little bit lost here.
Can anybody help me? Am I missing something?
Here is my code:
class Test_A(unittest.TestCase):
def doSomething(self):
print("class A")
@classmethod
def setUpClass(cls):
doSomething()
def test_1(self):
print("test 1")
def test_2(self):
print("test 2")
I am using python 2.7
python python-2.7
python python-2.7
edited Nov 16 '18 at 19:20
Johnny Chacon
asked Nov 16 '18 at 18:48
Johnny ChaconJohnny Chacon
34
34
closed as unclear what you're asking by user2357112, petezurich, Billal Begueradj, Rob, Patrick Mevzek Nov 17 '18 at 3:11
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by user2357112, petezurich, Billal Begueradj, Rob, Patrick Mevzek Nov 17 '18 at 3:11
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1
doSomething()
calls a global name, not a method on the class. You can't callcls.doSomething()
because you defineddoSomething
as a regular method, not a class or static method.
– Martijn Pieters♦
Nov 16 '18 at 18:50
1
What instance ofTest_A
do you want to execute thedoSomething
method of?
– user2357112
Nov 16 '18 at 18:50
So, there is no any chance to call doSomething() from setUpClass() ?
– Johnny Chacon
Nov 16 '18 at 19:04
Actually, I have several instances of Test_A. Since it is for Unit Testing I only run the test methods and this is made automatically (it is a little bit complex for me to explain, but Unit Testing works in this way).
– Johnny Chacon
Nov 16 '18 at 19:06
I mean, how I can make setUpClass() call doSomething() ?
– Johnny Chacon
Nov 16 '18 at 19:17
|
show 2 more comments
1
doSomething()
calls a global name, not a method on the class. You can't callcls.doSomething()
because you defineddoSomething
as a regular method, not a class or static method.
– Martijn Pieters♦
Nov 16 '18 at 18:50
1
What instance ofTest_A
do you want to execute thedoSomething
method of?
– user2357112
Nov 16 '18 at 18:50
So, there is no any chance to call doSomething() from setUpClass() ?
– Johnny Chacon
Nov 16 '18 at 19:04
Actually, I have several instances of Test_A. Since it is for Unit Testing I only run the test methods and this is made automatically (it is a little bit complex for me to explain, but Unit Testing works in this way).
– Johnny Chacon
Nov 16 '18 at 19:06
I mean, how I can make setUpClass() call doSomething() ?
– Johnny Chacon
Nov 16 '18 at 19:17
1
1
doSomething()
calls a global name, not a method on the class. You can't call cls.doSomething()
because you defined doSomething
as a regular method, not a class or static method.– Martijn Pieters♦
Nov 16 '18 at 18:50
doSomething()
calls a global name, not a method on the class. You can't call cls.doSomething()
because you defined doSomething
as a regular method, not a class or static method.– Martijn Pieters♦
Nov 16 '18 at 18:50
1
1
What instance of
Test_A
do you want to execute the doSomething
method of?– user2357112
Nov 16 '18 at 18:50
What instance of
Test_A
do you want to execute the doSomething
method of?– user2357112
Nov 16 '18 at 18:50
So, there is no any chance to call doSomething() from setUpClass() ?
– Johnny Chacon
Nov 16 '18 at 19:04
So, there is no any chance to call doSomething() from setUpClass() ?
– Johnny Chacon
Nov 16 '18 at 19:04
Actually, I have several instances of Test_A. Since it is for Unit Testing I only run the test methods and this is made automatically (it is a little bit complex for me to explain, but Unit Testing works in this way).
– Johnny Chacon
Nov 16 '18 at 19:06
Actually, I have several instances of Test_A. Since it is for Unit Testing I only run the test methods and this is made automatically (it is a little bit complex for me to explain, but Unit Testing works in this way).
– Johnny Chacon
Nov 16 '18 at 19:06
I mean, how I can make setUpClass() call doSomething() ?
– Johnny Chacon
Nov 16 '18 at 19:17
I mean, how I can make setUpClass() call doSomething() ?
– Johnny Chacon
Nov 16 '18 at 19:17
|
show 2 more comments
1 Answer
1
active
oldest
votes
Follow the advice from Martijn:
class Test_A(unittest.TestCase):
@classmethod
def doSomething(self):
print("class A")
@classmethod
def setUpClass(cls):
cls.doSomething()
Test_A.setUpClass()
Output:
class A
cls
is the implicit first argument of the call. Just as self
is our convention for the object invoking an instance method, cls
is used for the class calling the class method.
This is convention, not a language requirement. You're free to use any valid identifier: self
for everything, or caller
, or even xyzzy
or plugh
. The convention merely makes it easier for others to recognize the calling object.
Thanks very much! Actually (believe it or not) I was answering my own question when you posted yours. In the beginning, I didn't catch what Martijn was trying to say, but after a few experiments with my code, I was able to understand what he was trying to tell me. doSomething() needed to be a class method to work. Thank you very much. Also thanks for clarifying my doubt with cls, I thought it was some language requirement.
– Johnny Chacon
Nov 16 '18 at 19:32
Glad to be of help. Remember to upvote useful stuff as well. I did so with Martijn's comment (after waiting to see whether he'd post it as an answer).
– Prune
Nov 16 '18 at 19:35
Sure! Actually I was looking some button to upvote his comment but I didn't find it, I suppose only answers and no comments can be upvoted. Really thank you very much as also thanks to Martijn, thanks to you people I am studying programming. Take care.
– Johnny Chacon
Nov 16 '18 at 19:39
Ah ... you need 15 reputation for upvoting comments.
– Prune
Nov 16 '18 at 19:44
Here everything needs a lot of reputation points, is not fair haha.
– Johnny Chacon
Nov 16 '18 at 20:19
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Follow the advice from Martijn:
class Test_A(unittest.TestCase):
@classmethod
def doSomething(self):
print("class A")
@classmethod
def setUpClass(cls):
cls.doSomething()
Test_A.setUpClass()
Output:
class A
cls
is the implicit first argument of the call. Just as self
is our convention for the object invoking an instance method, cls
is used for the class calling the class method.
This is convention, not a language requirement. You're free to use any valid identifier: self
for everything, or caller
, or even xyzzy
or plugh
. The convention merely makes it easier for others to recognize the calling object.
Thanks very much! Actually (believe it or not) I was answering my own question when you posted yours. In the beginning, I didn't catch what Martijn was trying to say, but after a few experiments with my code, I was able to understand what he was trying to tell me. doSomething() needed to be a class method to work. Thank you very much. Also thanks for clarifying my doubt with cls, I thought it was some language requirement.
– Johnny Chacon
Nov 16 '18 at 19:32
Glad to be of help. Remember to upvote useful stuff as well. I did so with Martijn's comment (after waiting to see whether he'd post it as an answer).
– Prune
Nov 16 '18 at 19:35
Sure! Actually I was looking some button to upvote his comment but I didn't find it, I suppose only answers and no comments can be upvoted. Really thank you very much as also thanks to Martijn, thanks to you people I am studying programming. Take care.
– Johnny Chacon
Nov 16 '18 at 19:39
Ah ... you need 15 reputation for upvoting comments.
– Prune
Nov 16 '18 at 19:44
Here everything needs a lot of reputation points, is not fair haha.
– Johnny Chacon
Nov 16 '18 at 20:19
add a comment |
Follow the advice from Martijn:
class Test_A(unittest.TestCase):
@classmethod
def doSomething(self):
print("class A")
@classmethod
def setUpClass(cls):
cls.doSomething()
Test_A.setUpClass()
Output:
class A
cls
is the implicit first argument of the call. Just as self
is our convention for the object invoking an instance method, cls
is used for the class calling the class method.
This is convention, not a language requirement. You're free to use any valid identifier: self
for everything, or caller
, or even xyzzy
or plugh
. The convention merely makes it easier for others to recognize the calling object.
Thanks very much! Actually (believe it or not) I was answering my own question when you posted yours. In the beginning, I didn't catch what Martijn was trying to say, but after a few experiments with my code, I was able to understand what he was trying to tell me. doSomething() needed to be a class method to work. Thank you very much. Also thanks for clarifying my doubt with cls, I thought it was some language requirement.
– Johnny Chacon
Nov 16 '18 at 19:32
Glad to be of help. Remember to upvote useful stuff as well. I did so with Martijn's comment (after waiting to see whether he'd post it as an answer).
– Prune
Nov 16 '18 at 19:35
Sure! Actually I was looking some button to upvote his comment but I didn't find it, I suppose only answers and no comments can be upvoted. Really thank you very much as also thanks to Martijn, thanks to you people I am studying programming. Take care.
– Johnny Chacon
Nov 16 '18 at 19:39
Ah ... you need 15 reputation for upvoting comments.
– Prune
Nov 16 '18 at 19:44
Here everything needs a lot of reputation points, is not fair haha.
– Johnny Chacon
Nov 16 '18 at 20:19
add a comment |
Follow the advice from Martijn:
class Test_A(unittest.TestCase):
@classmethod
def doSomething(self):
print("class A")
@classmethod
def setUpClass(cls):
cls.doSomething()
Test_A.setUpClass()
Output:
class A
cls
is the implicit first argument of the call. Just as self
is our convention for the object invoking an instance method, cls
is used for the class calling the class method.
This is convention, not a language requirement. You're free to use any valid identifier: self
for everything, or caller
, or even xyzzy
or plugh
. The convention merely makes it easier for others to recognize the calling object.
Follow the advice from Martijn:
class Test_A(unittest.TestCase):
@classmethod
def doSomething(self):
print("class A")
@classmethod
def setUpClass(cls):
cls.doSomething()
Test_A.setUpClass()
Output:
class A
cls
is the implicit first argument of the call. Just as self
is our convention for the object invoking an instance method, cls
is used for the class calling the class method.
This is convention, not a language requirement. You're free to use any valid identifier: self
for everything, or caller
, or even xyzzy
or plugh
. The convention merely makes it easier for others to recognize the calling object.
answered Nov 16 '18 at 19:21
PrunePrune
46k143659
46k143659
Thanks very much! Actually (believe it or not) I was answering my own question when you posted yours. In the beginning, I didn't catch what Martijn was trying to say, but after a few experiments with my code, I was able to understand what he was trying to tell me. doSomething() needed to be a class method to work. Thank you very much. Also thanks for clarifying my doubt with cls, I thought it was some language requirement.
– Johnny Chacon
Nov 16 '18 at 19:32
Glad to be of help. Remember to upvote useful stuff as well. I did so with Martijn's comment (after waiting to see whether he'd post it as an answer).
– Prune
Nov 16 '18 at 19:35
Sure! Actually I was looking some button to upvote his comment but I didn't find it, I suppose only answers and no comments can be upvoted. Really thank you very much as also thanks to Martijn, thanks to you people I am studying programming. Take care.
– Johnny Chacon
Nov 16 '18 at 19:39
Ah ... you need 15 reputation for upvoting comments.
– Prune
Nov 16 '18 at 19:44
Here everything needs a lot of reputation points, is not fair haha.
– Johnny Chacon
Nov 16 '18 at 20:19
add a comment |
Thanks very much! Actually (believe it or not) I was answering my own question when you posted yours. In the beginning, I didn't catch what Martijn was trying to say, but after a few experiments with my code, I was able to understand what he was trying to tell me. doSomething() needed to be a class method to work. Thank you very much. Also thanks for clarifying my doubt with cls, I thought it was some language requirement.
– Johnny Chacon
Nov 16 '18 at 19:32
Glad to be of help. Remember to upvote useful stuff as well. I did so with Martijn's comment (after waiting to see whether he'd post it as an answer).
– Prune
Nov 16 '18 at 19:35
Sure! Actually I was looking some button to upvote his comment but I didn't find it, I suppose only answers and no comments can be upvoted. Really thank you very much as also thanks to Martijn, thanks to you people I am studying programming. Take care.
– Johnny Chacon
Nov 16 '18 at 19:39
Ah ... you need 15 reputation for upvoting comments.
– Prune
Nov 16 '18 at 19:44
Here everything needs a lot of reputation points, is not fair haha.
– Johnny Chacon
Nov 16 '18 at 20:19
Thanks very much! Actually (believe it or not) I was answering my own question when you posted yours. In the beginning, I didn't catch what Martijn was trying to say, but after a few experiments with my code, I was able to understand what he was trying to tell me. doSomething() needed to be a class method to work. Thank you very much. Also thanks for clarifying my doubt with cls, I thought it was some language requirement.
– Johnny Chacon
Nov 16 '18 at 19:32
Thanks very much! Actually (believe it or not) I was answering my own question when you posted yours. In the beginning, I didn't catch what Martijn was trying to say, but after a few experiments with my code, I was able to understand what he was trying to tell me. doSomething() needed to be a class method to work. Thank you very much. Also thanks for clarifying my doubt with cls, I thought it was some language requirement.
– Johnny Chacon
Nov 16 '18 at 19:32
Glad to be of help. Remember to upvote useful stuff as well. I did so with Martijn's comment (after waiting to see whether he'd post it as an answer).
– Prune
Nov 16 '18 at 19:35
Glad to be of help. Remember to upvote useful stuff as well. I did so with Martijn's comment (after waiting to see whether he'd post it as an answer).
– Prune
Nov 16 '18 at 19:35
Sure! Actually I was looking some button to upvote his comment but I didn't find it, I suppose only answers and no comments can be upvoted. Really thank you very much as also thanks to Martijn, thanks to you people I am studying programming. Take care.
– Johnny Chacon
Nov 16 '18 at 19:39
Sure! Actually I was looking some button to upvote his comment but I didn't find it, I suppose only answers and no comments can be upvoted. Really thank you very much as also thanks to Martijn, thanks to you people I am studying programming. Take care.
– Johnny Chacon
Nov 16 '18 at 19:39
Ah ... you need 15 reputation for upvoting comments.
– Prune
Nov 16 '18 at 19:44
Ah ... you need 15 reputation for upvoting comments.
– Prune
Nov 16 '18 at 19:44
Here everything needs a lot of reputation points, is not fair haha.
– Johnny Chacon
Nov 16 '18 at 20:19
Here everything needs a lot of reputation points, is not fair haha.
– Johnny Chacon
Nov 16 '18 at 20:19
add a comment |
1
doSomething()
calls a global name, not a method on the class. You can't callcls.doSomething()
because you defineddoSomething
as a regular method, not a class or static method.– Martijn Pieters♦
Nov 16 '18 at 18:50
1
What instance of
Test_A
do you want to execute thedoSomething
method of?– user2357112
Nov 16 '18 at 18:50
So, there is no any chance to call doSomething() from setUpClass() ?
– Johnny Chacon
Nov 16 '18 at 19:04
Actually, I have several instances of Test_A. Since it is for Unit Testing I only run the test methods and this is made automatically (it is a little bit complex for me to explain, but Unit Testing works in this way).
– Johnny Chacon
Nov 16 '18 at 19:06
I mean, how I can make setUpClass() call doSomething() ?
– Johnny Chacon
Nov 16 '18 at 19:17