def test_A(self, mocked_get, mocked_print): Response = namedtuple('Response', ['data']) response = Response(data={'has_value': True}) mocked_get.side_effect = [response, 'new response'] myclass = MyClass() data = {} myclass.A(data) mocked_get.assert_called_with('some_url', None) self.assertEqual(mocked_get.call_count, 2) mocked_print.assert_called_with('new response')
def main(): print(f"myscript attributes...") print(f"Module: {__name__}") print(f"Package: {__package__}") print(f"File Path: {__file__}") print(f"sys.path: {sys.path}") print() mc = MyClass() mc.dump()
def main(): from pycompss.api.api import compss_wait_on o = MyClass(10) o.modify(2) o = compss_wait_on(o) print("o.a: " + str(o.a)) if 20 == o.a: print("- Result value: OK") else: print("- Result value: ERROR")
class TestSequenceFunctions(unittest.TestCase): def setUp(self): self.something = MyClass("pekka") def test_return(self): name = self.something.get_name() self.assertEqual(name, "pekka")
def test__run(self): myclass = MyClass() myclass.A = MagicMock() myclass.B = MagicMock() myclass._run() myclass.A.assert_called_with({}) myclass.B.assert_called_with({})
def testSynchronise(self): myclass = MyClass() # put the monkey patching in place myclass.getDataSource = Mock() myclass.getDataSource.return_value = sentinel.DataSource myclass.readData = Mock() myclass.store = Mock() # make the call myclass.synchronise() # assertions self.assertTrue(myclass.getDataSource.called) myclass.readData.assert_called_with(sentinel.DataSource) self.assertTrue(myclass.store.called)
class MyTest(unittest.TestCase): # 继承父类 # 初始化工作 def setUp(self): self.tclass = MyClass() # 实例化了被测试模块中的类 # 退出清理工作 def tearDown(self): pass # 具体的测试用例,一定要以test开头 def sametest(self, par, ret): print "test sum" self.assertEqual(self.tclass.sum(1, par), ret) def testsum(self): # self.assertEqual(self.tclass.sum(1, 2), 2) for i in [(1, 2), (2, 2), (3, 3)]: self.sametest(i[0], i[1]) def test_upper(self): self.assertEqual('foo'.upper(), 'FOO') def test_isupper(self): self.assertTrue('FOO'.isupper()) self.assertFalse('Foo'.isupper()) def test_split(self): s = 'hello world' self.assertEqual(s.split(), ['hello', 'world']) # check that s.split fails when the separator is not a string with self.assertRaises(TypeError): # ?? s.split(2) def test_even(self): """ Test that numbers between 0 and 5 are all even. """ for i in range(0, 6): # with self.subTest(i=i): self.assertEqual(i % 2, 0)
def setUp(self): self.something = MyClass("pekka")
def changeme(list, age=1): "修改传入的列表" mylist.append([1, 2, 3, 4]) print "函数内取值: ", mylist print "age=", age return # 调用changeme函数 mylist = [10, 20, 30] changeme(list=mylist, age=3) print "函数外取值: ", mylist # dir()的参数必须是用import 引入的模块 content = dir(module_test) print content # 可以通过module.funcname的方式调用模块中的函数或变量 module_test.addmoney() # 用from。。。import。。。引入的函数可以直接调用 addmoney() print "当前目录:", os.getcwd() # 类实例化 obj = MyClass() obj.readStr() obj.writeStrToFile()
def my_function(): mc = MyClass() mc.hello()
from myclass import MyClass s = MyClass("Eldar", 28)
# 可写函数说明 def changeme(list, age=1): "修改传入的列表" mylist.append([1, 2, 3, 4]) print "函数内取值: ", mylist print "age=", age return # 调用changeme函数 mylist = [10, 20, 30] changeme(list=mylist, age=3) print "函数外取值: ", mylist #dir()的参数必须是用import 引入的模块 content = dir(module_test) print content #可以通过module.funcname的方式调用模块中的函数或变量 module_test.addmoney() #用from。。。import。。。引入的函数可以直接调用 addmoney() print '当前目录:', os.getcwd() #类实例化 obj = MyClass() obj.readStr() obj.writeStrToFile()
#! /usr/bin/env python2 from myclass import MyClass myObj = MyClass(foo='bar') print vars(myObj) myObj.kaboom()
from myclass import MyClass from myclass2 import MyClass2 s = MyClass('asdasdas') n = MyClass2('jaskldjklasjd') # s.read() # s.change('jaksldjalksdjqi') # s.read() n.read() n.change('jaksldjalksdjqi111') n.read()
data[deviceName][metricName]["ruleId"] = ruleId data[deviceName][metricName][ "sampleDataTimeRange"] = sampleDataTimeRange data[deviceName][metricName]["lastestTime"] = time.time() del data[deviceName][metricName]["timeout"] return "SUCCESS" def handle4(self, task): print data aaaa = json.dumps(data) return aaaa # print(12) t = MyClass() t.start() # print(11) cf = configparser.ConfigParser() cf.read("python.ini") redis_server = cf.get("register", "address") host = cf.get("mysql", "host").encode() port = int(cf.get("mysql", "port")) user = cf.get("mysql", "user").encode() passwd = cf.get("mysql", "passwd").encode() db = cf.get("mysql", "db").encode() charset = cf.get("mysql", "charset").encode() minute = int(cf.get("mysql", "minute")) Polling(data, minute, host, port, user, passwd, db, charset).run()
def setUp(self): self.tclass = MyClass() # 实例化了被测试模块中的类
from myclass import MyClass a = MyClass("Auyeskhan", "Qostanay", "teacher") a.meth_1() a.meth_2() a.meth_1()
from mybox import Mybox from myboxiterator import MyBoxIterator from myclass import MyClass b=MyBox() b.add(MyClass('Symbat', 'Karaganda', 'Teacher')) b.add(MyClass('Den', 'Moskva', 'Driver')) b.add(MyClass('Zere', 'Almaty', 'Student')) b.remove(1) for i in b: i.meth_1()
def create_object(value): if value == 'myclass': return MyClass() else: return NullClass()
from myclass import MyClass class MyIterator(object): def __init__(self): self.myclasses = [] self.index = 0 def add(self, myclass): self.myclasses.append(myclass) return self def next(self): if self.index >= len(self.myclasses): self.index=0 raise StopIteration ret=self.myclasses[self.index] self.index += 1 return ret def __iter__(self): return self my=MyIterator().add(MyClass(1, "foo1")).add(MyClass(2, "foo2")).add(MyClass(3, "foo3")) for hoge in my: print(hoge) print [x.name for x in my if x.id == 2]