def test_add(a, b, expected): from calculate import add result = add(a, b) assert result == pytest.approx(expected) #def test_sub(a, b, expected): # from calculate import sub # result = sub(a, b) # assert result == pytest.approx(expected)
def runAdd(): print("This is Add function") print("result= 5+3="+str(calculate.add(5, 3))) return
user包: |——models.py |——User |——__init__.py |——test.py package.py(同级) """ # 外用里 # 用户发表文章 # 创建用户对象 # 即使两个模块在同一个包里,导入时也应该通过包.模块导入 from uers.models import User # from .models import User # 当前目录下的models里面的User类 # ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package from article.models import Article user = User('admin', '123456') # ----->user就是通过导入User类创建的 # 发表文章,文章对象 article = Article('个人总结', '家伟') user.publish_article(article) # 里用外 list1 = [1, 3, 5, 6, 7] from calculate import add result = add(*list1) print(result)
def test_add(self): # https://docs.python.org/3/library/unittest.html result = calculate.add(10, 5) self.assertEqual(result, 15)
def test_add(a, b, expected): from calculate import add print("a={},b={}".format(a, b)) result = add(a, b) assert result == pytest.approx(expected)
import calculate number = 10 number2 = 2 print(calculate.add(number, number2)) print(calculate.subtract(number, number2)) print(calculate.multiplied(number, number2)) print(calculate.divide(number, number2))
def test_add(self): self.assertEqual(3, add(1, 2), msg='测试失败时打印的信息')
import calculate as clt ch = 1 while ch == 1: var1 = int(input("enter the first variable")) var2 = int(input("enter the second variable")) print(clt.add(var1, var2)) print(clt.sub(var1, var2)) print(clt.div(var1, var2)) print(clt.mul(var1, var2)) ch = int(input("do you want to continue...?"))
# 搜索路径:sys.path import calculate from calculate import add, sub #从模块中调用方法, print(calculate.add(1, 2)) print(add(2, 4)) print(sub(2, 3)) import Web # print(Web.main.x) module 'Web' has no attribute 'main' from Web import main print(main.x)
1 import 模块名 调用时用 模块名. 2 from 模块名 import 变量/函数/类 3 from 模块名 import * 引入该模块中所有的内容。如果需要限制。__all__ = ['','','',''] 4 无论from 还是import 模块在导入时会加载所有的内容和执行。 如果不希望调用,就会用到__name__ ''' import random # 导入模块 这样调用是 模块.成员。 import calculate from calculate import add from calculate import number, name, Calculater # for i in range(1,10): # r = random.randint(1, 10) # print(r) # 调用模块里的函数 模块名.变量/函数/类。 list1 = [2, 3, 4, 5] print(calculate.add(*(list1))) print('calculate.number', calculate.number) calculate.Calculater(10).test() calculate.Calculater.test1() calculate.Calculater.test2() print(add(1, 3, 43, number)) print(name) Calculater(10).test()
import calculate calculate.add(34, 56)
import calculate result = calculate.add(1, 2) print(result)
import calculate calculate.add(34,56)
import calculate as cal # 匯入 calculate 模組,並取別名為 cal print(cal.add(5,2)) # 7 print(cal.sub(5,2)) # 3
import calculate if __name__ == '__main__': print('result:', calculate.add(3, 4))
def test_addition(self): assert 4 == calculate.add(2, 2)
def test_add(self): self.assertEqual(3, add(1, 2)) self.assertNotEqual(3, add(2, 2))
import calculate import datetime print(datetime.time) value = calculate.add(5, 6) print(value)
import calculate print(calculate.multiply(256837,4)) print(calculate.add(727225,2737532)) print(calculate.divide(6,3)) print(calculate.subtract(9,2))
def test_add(a, b, expected): from calculate import add result = add(a, b) assert result == pytest.approx(expected)
def test_add(self): self.assertEqual(10, add(3, 7), msg="加法出错")
import calculate calculate.add(5,6)
from calculate import add,sub print(add(5,2)) # 7 print(sub(5,2)) # 3
# 模块 ''' 1.自定义模块 2.使用系统一些模块 导入模块: 1. import 模块名 模块名.函数 模块名.变量 模块名.类 2. from 模块名 import 变量|函数|类 ''' import calculate as cal list = [4, 5, 6, 7, 8] result = cal.add(*list) print(result) print(cal.number) print(cal.Student)
def _(): assert calculate.add(1, 2) == 3
s.age = 10 print(s.age) # 单例模式 class Singleton: __instance = None def __new__(cls, *args, **kwargs): if cls.__instance is None: cls.__instance = object.__new__(cls) return cls.__instance n1 = Singleton() n2 = Singleton() print(n1, n2) print(n1 == n2) # 模块 # import calculate # list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9] # res=calculate.add(*list1) # print(res) from calculate import add res = add(*list1) print(res)
def _(): assert calculate.add(1, 1) == 2
import calculate number = 10 number1 = 2 print(calculate.add(10, 2)) print(calculate.subtract(10, 2)) print(calculate.multiplied(10, 2)) print(calculate.divide(10, 2))
import calculate a = int(input("enter the first number")) b = int(input("enter the second number")) calculate.add(a, b) calculate.sub(a, b)
def test_add(self): self.assertEqual(calculate.add(5, 10), 15)
import calculate # 匯入 calculate 模組 print(calculate.add(5,2)) # 7 print(calculate.sub(5,2)) # 3