I am trying to use mock.patch
decorator in the mock library but am having problems. I have this init function where I make two subprocess calls. The first time in self.setPackageQuota()
and the second time in self.setBandwidthLimit()
.
class User():
def __init__(self, username, plan, domain, owner, diskQuota, diskUsed):
self.setUsername(username)
self.setPlan(plan)
self.setDomain(domain)
self.setOwner(owner)
self.setDiskQuota(diskQuota)
self.setDiskUsed(diskUsed)
self.setPackageQuota()
self.setBandwidthLimit()
self.setPackageQuota()
def setBandwidthLimit(self):
whmapicall = subprocess.Popen(["whmapi1" , "showbw", 'searchtype=user', 'search=^%s$' % self.username], stdout=subprocess.PIPE)
whmapireturn = whmapicall.stdout.read().split("\n")
for line in whmapireturn:
self.setPackageQuota()
def setPackageQuota(self):
whmapicall = subprocess.Popen(["whmapi1" , "getpkginfo", "pkg=%s" % self.plan], stdout=subprocess.PIPE)
whmapireturn = whmapicall.stdout.read().split("\n")
for line in whmapireturn:
I would much rather patch whmapireturn
to be something else. I also would not like to not run subprocess.Popen
. My inital thought was to patch out @mock.patch('subprocess.Popen', MockedClass)
and @mock.patch('whmapireturn', OtherMockedClass)
but I cant seem to get it to work. How would I test an init fucntion like this while patching out things that I can't have in my enviroment? Thanks in advance for any asistance.
I comprehend from your question that you want to mock-patch the Subprocess.Popen() call from your file. For that you should use the following approach:-
Let take the scenario where your file name is for which you want to create the unittest. Therefore:- in the unittest file, you should write:-
import library
@mock.patch("library.subprocess")
def test_subprocess_call(mock_subprocess):
mock_subprocess.Popen.return_value = None
I hope you find your answer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.