Calling a test method in another TestClass - python

Hi I have the following test classes
logintests.py:
class SignOnTests(BaseTest):
def test_login_info(self):
#some code
passwordtests.py:
class ResetPasswordTests(unittest.TestCase):
def test_mytest(self):
#somecode
#Should Call SignOnTests.test_login_info()
I've tried putting on passwordtests.py:
from logintests import SignOnTests
But keep gettingTypeError: unbound method test_login_info() must be called with SignOnTests instance as first argument (got nothing instead)
Is it possible to call a test method within another module?

Related

patching instantiated object method call in python

Suppose we have
import XYZ
Class(object):
def __init__(self):
pass
def some_call(self)
#other logic
obj = XYZ()
obj.do_something()
In my unittest I'm trying to patch XYZ, and the call to do_something. I want to call some_call in my unittest because I have other logic to perform, but specifically the one instance and method call on the instance I want to bypass.

Does a method of a class should have at least one parameter referring to an instance?

Python In a Nutshell says that:
a method defined in a class body has a mandatory first
parameter, conventionally named self , that refers to the instance
on which you call the method.
Does a method of a class should have at least one parameter referring to an instance?
Is it a bad practice to create a method without any parameter?
Is it a good practice to always make a method work when calling it on either the class or an instance of the class?
Note that I am not talking about a static method or class method, but an ordinary method.
>>> class C5(object):
... def hello():
... print('Hello')
...
>>> C5.hello()
Hello
>>> C5().hello()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: hello() takes 0 positional arguments but 1 was given
Thanks.
You should always put self as the first parameter in the class method. If you want call class method without creating a class instance, need to write following:
class Foo:
#staticmethod
def hello():
print("Hi")
Python wants you to pass self as an argument into your class methods so that python knows that the methods belong to the class that they are in.

Calling class methods in another python program - Unbound error issue

I have a program1.py that has the following structure:
program1.py
class program1_class()
def __init(self,var1):
command1
def method2(self,var1):
Then I have a program2 that imports the the class and needs to access the method2
program2.py
from program1_class import program1_class()
def method2(var1):
call_method2 = program1_class.method2(var1)
When I do this, I get the error
TypeError: unbound method predict_prob() must be called with LogisticRegressionSGD instance as first argument (got list instance instead)
There are a couple problems here-
from program1_class import program1_class() is invalid syntax in several ways.
The correct import statement is
from program1 import program1_class
Second, you tried to call a bound instance method on a class.
call_method2 = program1_class.method2(var1)
If you look at program1 you'll see that method2 is defined on the instance. You can tell this because the first argument is self. You call this by intantiating an instance of the class and then calling the method.
call_method2 = program1_class().method2(var1)

How to call internal functions inside the constructor class?

Hello i have this code
class Test(object):
def start_conn(self):
pass
def __init__(self):
self.conn = start_conn()
But this code make this error:
NameError: global name 'start_conn' is not defined
If i write self.conn = self.start_conn() the program works without error, my question is, is a must to call with self the methods of the class when i'm creating a new instance? or is a desgin error from my side?
Thanks a lot
In short, it's a must. You have to refer to the container in which the methods are stored. Most of the time that means referring to self.
The way this works is as follows. When you define a (new-style) class
class FooClass(object):
def my_method(self, arg):
print self.my_method, arg
you create a type object that contains the method in its unbound state. You can then refer to that unbound method via the name of the class (i.e. via FooClass.my_method); but to use the method, you have to explicitly pass a FooClass object via the self parameter (as in FooClass.my_method(fooclass_instance, arg)).
Then, when you instantiate your class (f = FooClass()), the methods of FooClass are bound to the particular instance f. self in each of the methods then refers to that instance (f); this is automatic, so you no longer have to pass f into the method explicitly. But you could still do FooClass.my_method(f, arg); that would be equivalent to f.my_method(arg).
Note, however, that in both cases, self is the container through which the other methods of the class are passed to my_method, which doesn't have access to them through any other avenue.

Self in Class Demanding Argument

For some reason most instances of classes are returning Type errors saying that insufficient arguments were passed, the problem is with self.
This works fine:
class ExampleClass:
def __init__(self, some_message):
self.message = some_message
print ("New ExampleClass instance created, with message:")
print (self.message)
ex = ExampleClass("message")
However almost every other Class I define and call an instance of returns the same error. The almost identical function:
class Test(object):
def __init__(self):
self.defaultmsg = "message"
def say(self):
print(self.defaultmsg)
test = Test
test.say()
Returns a Type Error, saying that it needs an argument. I'm getting this problem not just with that class, but with pretty much every class I define, and I have no idea what the problem is. I just updated python, but was getting the error before. I'm fairly new to programming.
You have to instantiate the class:
test = Test() #test is an instance of class Test
instead of
test = Test #test is the class Test
test.say() #TypeError: unbound method say() must be called with Test instance as first argum ent (got nothing instead)
if you are curious you can try this:
test = Test
test.say(Test()) #same as Test.say(Test())
It works because I gave the class instance (self) to the unbound method !
Absolutely not recommended to code this way.
You should add parentheses to instantiate a class:
test = Test()
Your test refers to the class itself, rather than an instance of that class. To create an actual test instance, or to 'instantiate' it, add the parentheses. For example:
>>> class Foo(object):
... pass
...
>>> Foo
<class '__main__.Foo'>
>>> Foo()
<__main__.Foo object at 0xa2eb50>
The error message was trying to tell you that there was no such self object to pass in (implicitly) as the function argument, because in your case test.say would be an unbound method.

Categories

Resources