鱼C论坛

 找回密码
 立即注册
查看: 980|回复: 7

[已解决]为什么不运行测试

[复制链接]
发表于 2021-8-30 11:21:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
请教鱼油高手:

我编辑了一个模块,模块名temperature,代码如下:
def fahrenheit_to_celsius(fahrenheit:float)->float:
    """the number of fahrenheit temperature\
convert to the celsius one"""
    celsius_tem = (fahrenheit-32)*5/9
    return celsius_tem

def above_freezing(celsius:float)->bool:
    """return True when the celsius temperature\
above zero"""
    return celsius > 0

if __name__ == '__main__':

    fahrenheit = float(input('Pls key in the Fahrenheit temperature:'))
    celsius = fahrenheit_to_celsius(fahrenheit)
    if above_freezing(celsius):
        
        print('The temperature is above freezing')

    else:
        print('The temperature is below freezing')

然后我用doctest去测试,但是不运行(attempted=0):

Pls key in the Fahrenheit temperature:500
The temperature is above freezing
>>> import doctest
>>> doctest.testmod()
TestResults(failed=0, attempted=0)
>>>
模块里有三个函数,照道理应该调用三次,就是attempted = 3, 但是为什么会这样?


最佳答案
2021-9-1 09:42:19
本帖最后由 suchocolate 于 2021-9-1 11:17 编辑

两文件放到一个文件夹下:
temperature.py
  1. def fahrenheit_to_celsius(fahrenheit: float) -> float:
  2.     celsius_tem = (fahrenheit - 32) * 5 / 9
  3.     return celsius_tem


  4. def above_freezing(celsius: float) -> bool:
  5.     return celsius > 0
复制代码


test.txt
  1. >>> from temperature import *
  2. >>> fahrenheit_to_celsius(80)
  3. 26.666666666666668
  4. >>> above_freezing(30)
  5. True
复制代码


cmd跳到相应路径,执行:
  1. python -m doctest -v test.txt
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-8-30 14:15:36 | 显示全部楼层
看了好些遍  都没看明白你想问啥
ps  为啥全都是英文啊   看着很不好,难道你不是大陆的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-30 16:55:58 | 显示全部楼层
wp231957 发表于 2021-8-30 14:15
看了好些遍  都没看明白你想问啥
ps  为啥全都是英文啊   看着很不好,难道你不是大陆的

不好意思!主要是使用习惯了。这个模块包含三个函数,华氏温度转摄氏读数,摄氏温度转华氏温度,是否结冰。如果用dotest测试代码,应该attempted=3才对,可是这里为0。我怀疑是否模块程序有误。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-1 09:42:19 From FishC Mobile | 显示全部楼层    本楼为最佳答案   
本帖最后由 suchocolate 于 2021-9-1 11:17 编辑

两文件放到一个文件夹下:
temperature.py
  1. def fahrenheit_to_celsius(fahrenheit: float) -> float:
  2.     celsius_tem = (fahrenheit - 32) * 5 / 9
  3.     return celsius_tem


  4. def above_freezing(celsius: float) -> bool:
  5.     return celsius > 0
复制代码


test.txt
  1. >>> from temperature import *
  2. >>> fahrenheit_to_celsius(80)
  3. 26.666666666666668
  4. >>> above_freezing(30)
  5. True
复制代码


cmd跳到相应路径,执行:
  1. python -m doctest -v test.txt
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-2 10:22:51 | 显示全部楼层
suchocolate 发表于 2021-9-1 09:42
两文件放到一个文件夹下:
temperature.py

谢谢!我再试试
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-2 10:25:44 | 显示全部楼层
suchocolate 发表于 2021-9-1 09:42
两文件放到一个文件夹下:
temperature.py

不能在IDLE下运行吗?我不太会用DOS
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-2 10:37:15 | 显示全部楼层
suchocolate 发表于 2021-9-1 09:42
两文件放到一个文件夹下:
temperature.py

还是不行啊........
屏幕截图(35).png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-2 11:02:00 | 显示全部楼层
Geeker_odd 发表于 2021-9-2 10:37
还是不行啊........

cmd下不用进python
学程序cmd基础功能还是要会的。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-14 01:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表