标签: testing

我应该测试私有方法还是仅测试公共方法?

我读过这篇关于如何测试私有方法的帖子.我通常不测试它们,因为我一直认为只测试从对象外部调用的公共方法会更快.你测试私人方法吗?我应该经常测试吗?

language-agnostic testing unit-testing

329
推荐指数
16
解决办法
10万
查看次数

性能测试的准确时间测量

查看某些内容(例如方法调用)接受代码的最精确方式是什么?

我猜的最容易和最快的是:

DateTime start = DateTime.Now;
{
    // Do some work
}
TimeSpan timeItTook = DateTime.Now - start;
Run Code Online (Sandbox Code Playgroud)

但这有多精确?还有更好的方法吗?

.net c# testing performance

307
推荐指数
6
解决办法
21万
查看次数

如何使用JUnit Test注释断言我的异常消息?

我已经用@Test注释编写了一些JUnit测试.如果我的测试方法抛出一个已检查的异常,并且我想要将该消息与异常一起断言,那么有没有办法使用JUnit @Test注释?AFAIK,JUnit 4.7不提供此功能,但未来的版本是否提供此功能?我知道在.NET中你可以断言消息和异常类.寻找Java世界中的类似功能.

这就是我要的:

@Test (expected = RuntimeException.class, message = "Employee ID is null")
public void shouldThrowRuntimeExceptionWhenEmployeeIDisNull() {}
Run Code Online (Sandbox Code Playgroud)

java testing annotations junit4 assertion

286
推荐指数
8
解决办法
18万
查看次数

如何在RSpec中运行单个测试/规范文件?

我希望能够运行单个spec文件的测试 - 例如,我正在编辑的一个文件. rake spec执行所有规格.我的项目不是Rails项目,所以rake spec:doc不起作用.

不知道这是否重要,但这是我的目录结构.

./Rakefile
./lib
./lib/cushion.rb
./lib/cushion
./lib/cushion/doc.rb
./lib/cushion/db.rb
./spec
./spec/spec.opts
./spec/spec_helper.rb
./spec/db_spec.rb

ruby testing rspec

271
推荐指数
11
解决办法
20万
查看次数

如何在目录中运行所有Python单元测试?

我有一个包含我的Python单元测试的目录.每个单元测试模块的形式为test _*.py.我正在尝试创建一个名为all_test.py的文件,您猜对了,运行上述测试表单中的所有文件并返回结果.到目前为止,我尝试了两种方法; 都失败了.我将展示这两种方法,我希望那里的人知道如何正确地做到这一点.

对于我的第一次勇敢的尝试,我想"如果我只是在文件中导入我的所有测试模块,然后调用这个unittest.main()doodad,它会工作,对吧?" 好吧,事实证明我错了.

import glob
import unittest

testSuite = unittest.TestSuite()
test_file_strings = glob.glob('test_*.py')
module_strings = [str[0:len(str)-3] for str in test_file_strings]

if __name__ == "__main__":
     unittest.main()
Run Code Online (Sandbox Code Playgroud)

这不起作用,我得到的结果是:

$ python all_test.py 

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK
Run Code Online (Sandbox Code Playgroud)

对于我的第二次尝试,我可以,也许我会尝试以更"手动"的方式完成整个测试.所以我试着在下面这样做:

import glob
import unittest

testSuite = unittest.TestSuite()
test_file_strings = glob.glob('test_*.py')
module_strings = [str[0:len(str)-3] for str in test_file_strings]
[__import__(str) for str in module_strings]
suites = [unittest.TestLoader().loadTestsFromName(str) for str in module_strings]
[testSuite.addTest(suite) for suite in suites]
print …
Run Code Online (Sandbox Code Playgroud)

python testing unit-testing python-unittest

268
推荐指数
10
解决办法
18万
查看次数

什么是代码覆盖率以及您如何衡量它?

什么是代码覆盖率以及您如何衡量它?

我被问到有关我们自动化测试代码覆盖率的问题.似乎在自动化工具之外,它更多的是艺术,而不是科学.有没有关于如何使用代码覆盖的实际示例?

testing computer-science code-coverage

254
推荐指数
7
解决办法
17万
查看次数

单元测试的新手,如何编写出色的测试?

我对单元测试世界还很陌生,我刚刚决定在本周为我现有的应用添加测试覆盖率.

这是一项艰巨的任务,主要是因为要测试的课程数量,还因为编写测试对我来说都是新的.

我已经为一堆课程编写了测试,但现在我想知道我是否做得对.

当我为一个方法编写测试时,我感觉第二次重写我已经在方法本身中编写的内容.
我的测试似乎与方法紧密相关(测试所有代码路径,期望一些内部方法被调用多次,带有某些参数),似乎如果我重构该方法,即使该方法的最终行为没有改变.

这只是一种感觉,如前所述,我没有测试经验.如果一些更有经验的测试人员可以给我建议如何为现有应用程序编写出色的测试,那将非常感激.

编辑:我很想感谢Stack Overflow,我在15分钟内获得了很多投入,这些投入回答了我刚才在线阅读的更多时间.

testing unit-testing

252
推荐指数
7
解决办法
11万
查看次数

如何正确断言在pytest中引发异常?

码:

# coding=utf-8
import pytest


def whatever():
    return 9/0

def test_whatever():
    try:
        whatever()
    except ZeroDivisionError as exc:
        pytest.fail(exc, pytrace=True)
Run Code Online (Sandbox Code Playgroud)

输出:

================================ test session starts =================================
platform linux2 -- Python 2.7.3 -- py-1.4.20 -- pytest-2.5.2
plugins: django, cov
collected 1 items 

pytest_test.py F

====================================== FAILURES ======================================
___________________________________ test_whatever ____________________________________

    def test_whatever():
        try:
            whatever()
        except ZeroDivisionError as exc:
>           pytest.fail(exc, pytrace=True)
E           Failed: integer division or modulo by zero

pytest_test.py:12: Failed
============================== 1 failed in 1.16 seconds ==============================
Run Code Online (Sandbox Code Playgroud)

如何使pytest打印回溯,所以我会看到whatever函数中引发异常的位置?

python testing unit-testing pytest

246
推荐指数
8
解决办法
12万
查看次数

Kotlin和新的ActivityTestRule:@Rule必须是公开的

我正在尝试在Kotlin中为我的Android应用程序进行UI测试.由于使用ActivityTestRule的新系统,我无法使其工作:它正确编译,并在运行时,我得到:

java.lang.Exception: The @Rule 'mActivityRule' must be public.
    at org.junit.internal.runners.rules.RuleFieldValidator.addError(RuleFieldValidator.java:90)
    at org.junit.internal.runners.rules.RuleFieldValidator.validatePublic(RuleFieldValidator.java:67)
    at org.junit.internal.runners.rules.RuleFieldValidator.validateField(RuleFieldValidator.java:55)
    at org.junit.internal.runners.rules.RuleFieldValidator.validate(RuleFieldValidator.java:50)
    at org.junit.runners.BlockJUnit4ClassRunner.validateFields(BlockJUnit4ClassRunner.java:170)
    at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.ParentRunner.validate(ParentRunner.java:344)
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:74)
    at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:55)
    at android.support.test.internal.runner.junit4.AndroidJUnit4ClassRunner.<init>(AndroidJUnit4ClassRunner.java:38)
    at android.support.test.runner.AndroidJUnit4.<init>(AndroidJUnit4.java:36)
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
    at android.support.test.internal.runner.junit4.AndroidAnnotatedBuilder.buildAndroidRunner(AndroidAnnotatedBuilder.java:57)
    at android.support.test.internal.runner.junit4.AndroidAnnotatedBuilder.runnerForClass(AndroidAnnotatedBuilder.java:45)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
    at org.junit.runner.Computer.getRunner(Computer.java:38)
    at org.junit.runner.Computer$1.runnerForClass(Computer.java:29)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:98)
    at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:84)
    at org.junit.runners.Suite.<init>(Suite.java:79)
    at org.junit.runner.Computer.getSuite(Computer.java:26)
    at android.support.test.internal.runner.TestRequestBuilder.classes(TestRequestBuilder.java:691)
    at android.support.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:654)
    at android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:329)
    at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:226)
    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
Run Code Online (Sandbox Code Playgroud)

以下是我宣称的mActivityRule:

RunWith(javaClass<AndroidJUnit4>())
LargeTest
public class RadisTest {

    Rule
    public val mActivityRule: ActivityTestRule<MainActivity> = …
Run Code Online (Sandbox Code Playgroud)

testing android kotlin android-espresso

246
推荐指数
5
解决办法
3万
查看次数

使Android模拟器运行得更快

Android模拟器有点迟钝.对于某些设备,如Motorola Droid和Nexus One,应用程序在实际设备中的运行速度比模拟器快.这在测试游戏和视觉效果时是个问题.

如何使模拟器尽可能快地运行?我一直在玩弄它的参数,但还没有找到一个显示出明显改进的配置.

testing performance android emulation android-emulator

243
推荐指数
7
解决办法
19万
查看次数