我刚刚开始使用Coverage.py模块,所以决定进行一个简单的测试来检查它是如何工作的.
Sample.py
def sum(num1, num2):
return num1 + num2
def sum_only_positive(num1, num2):
if num1 > 0 and num2 > 0:
return num1 + num2
else:
return None
Run Code Online (Sandbox Code Playgroud)
test.py
from sample import sum, sum_only_positive
def test_sum():
assert sum(5, 5) == 10
def test_sum_positive_ok():
assert sum_only_positive(2, 2) == 4
def test_sum_positive_fail():
assert sum_only_positive(-1, 2) is None
Run Code Online (Sandbox Code Playgroud)
如你所见,我的所有代码都包含测试,py.test表示所有代码都通过了.我希望Coverage.py显示100%的覆盖率.好吧,不.
好吧,Coverage.py可能看不到test.py文件,所以我将测试函数复制到sample.py文件并再次运行Coverage:

然后我添加了这段代码:
if __name__ == "__main__":
print(sum(2, 4))
print(sum_only_positive(2, 4))
print(sum_only_positive(-1, 3))
Run Code Online (Sandbox Code Playgroud)
并删除所有测试功能.之后,Coverage.py显示100%:
为什么会这样?Coverage.py不应该显示代码测试覆盖率,而不仅仅是执行覆盖率?我已经阅读了Coverage.py 的官方常见问题解答,但无法找到解决方案.
由于许多SO用户都熟悉代码测试和代码覆盖,我希望你能告诉我,我在哪里弄错了.
我在这里只想到一个:Coverage.py可能只是看不执行哪行代码,所以我应该为这些行编写测试.但是有些行已经执行但未被测试覆盖,因此Coverage.py将在这里失败.
我有一个任务:在新行上打印%PATH%变量的所有条目.例如:
C:\Program Files\
C:\Windows
C:\Windows\System32
Run Code Online (Sandbox Code Playgroud)
等等...
试图解析一个 XML 文件让我感到厌烦。它不是我的,所以我无法编辑它。先看一下(我把不需要的部分剪掉了)
01 <feed>
02 <id>urn:ya.ru:feed/friends/12345678</id>
03 <author>
04 <name>Master</name>
05 <uri>http://test.ya.ru</uri>
06 <y:id>urn:ya.ru:person/12345678</y:id>
07 </author>
08 <title>List of friends posts</title>
09 <updated>2013-08-02T19:14:00Z</updated>
10 <entry>
11 <id>urn:ya.ru:post/110554367/3744</id>
12 <author>
13 <name>MrBigJoker</name>
14 <y:id>urn:ya.ru:person/110554367</y:id>
15 </author>
16 </entry>
17 </feed>
Run Code Online (Sandbox Code Playgroud)
您可以在第 02、06、11 和 14 行看到ID标签。问题是我在第 14 行收到错误“元素ID已被使用”。
我使用 SimpleXML 和以下三个类:
@Root (name = "feed")
public class Feed { // All lines
@Element
private String id;
@Element
private Author_feed author;
@Element
private String title;
@Element
private String …Run Code Online (Sandbox Code Playgroud) 抱歉我的英语不好,我会尽力尽可能简单地解释我的问题。我正在尝试制作一个与 Yandex API 配合使用的应用程序。在他们的帮助页面上,我读到您应该从用户登录的应用程序启动浏览器,然后通过注册 URI 回调返回到应用程序。我现在拥有的:
@Override
public void onClick(View view) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://oauth.yandex.ru/authorize?response_type=token&client_id=XXXXXXXXXXXXXXX"));
startActivity(browserIntent);
}
Run Code Online (Sandbox Code Playgroud)
这将启动浏览器并重定向到授权页面。输入登录密码后,我会自动返回到我的应用程序。这是 AndroidManifest:
<activity
android:name="ru.mastergroosha.yaruclient.Main"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name="ru.mastergroosha.yaruclient.Window"
android:label="Window">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data
android:scheme="yaruapp"
android:host="token"/>
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
当我输入登录密码时,我被重定向到“yaruapp://token#access_token=YYYYYYYYYYYYYYYY&token_type=code...”等页面。但我没有看到此页面,因为立即重定向回应用程序。
问题是:我如何获取并提取这部分: YYYYYYYYYYYYYY ?
我非常抱歉我这么菜鸟,希望你能帮助我。提前致谢
对不起,我的英语不好.
在我的应用程序中,我在共享首选项中保存令牌(它是一个Web应用程序).在第一个活动中,我这样做:
(token = 123)
SharedPreferences sp = getPreferences(MODE_PRIVATE);
Editor ed = sp.edit();
ed.putString("token", Main.getToken());
ed.commit();
Log.d("Recieved token: ", sp.getString("token", "null")); // Recieved token: 123
Run Code Online (Sandbox Code Playgroud)
如您所见,共享首选项已保存.
我有另一个活动,可以从浏览器调用以共享链接.
码:
sp = getPreferences(MODE_PRIVATE);
Log.d("Token recieved: ", sp.getString("token", "null")); // null
Run Code Online (Sandbox Code Playgroud)
但是在另一个活动上共享的prefs返回null.我能做什么?
android ×3
cmd ×1
coverage.py ×1
java ×1
oauth ×1
parsing ×1
python ×1
python-3.x ×1
unit-testing ×1
windows ×1
xml ×1