我正在使用JUnit 4测试@RunWith(value = Parameterized.class).这很好,没有问题.但是,当我的34个测试中的任何一个超时时,我只收到消息java.lang.Exception: test timed out after 15000 milliseconds.我希望它也显示测试的参数.
我甚至试图像下面的代码那样做(我知道这对于大多数情况来说是一个可怕的解决方案,我只是想看看我是否可以在任何时间显示消息),但这不起作用,它仍然导致上面的消息.
private String parameter;
@Test(timeout = 15000)
public void solveAll() {
try {
// ... do something that might take a long time
}
catch (Throwable e) {
Assert.fail(this.parameter + " failed! Because of " + e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
this.parameter当测试结果超时时,如何使JUnit也显示?
这是一个非常简单的示例测试类,它显示了这个问题:
public class ShowMyMessageTest {
@Test(timeout=1000)
public void test() {
try {
Thread.sleep(3000);
}
catch (Throwable e) {
Assert.fail("Timeout reached with value 42");
}
} …Run Code Online (Sandbox Code Playgroud) 我有几个不同的特征实现,我想测试,测试只使用特征的方法签名,所以我似乎应该能够使用参数化测试.但是,specs2网站似乎没有描述编写参数化测试的简单方法.最接近的是如何"共享示例",但您仍然需要编写测试和测试代码的每个组合,我希望能够指定:
A.测试
B.要测试的类
这可以单独指定,但将测试两者的笛卡尔积.
我是JUnit测试的新手,我想在IntelliJ IDEA 2017.3.3中创建一个参数化测试。所以我添加了JUnit 5:
然后IntelliJ下载org.junit.jupiter:junit-jupiter-api:5.0.0。现在,@Test正在工作,但@ParameterizedTest没有。它说“无法解析符号'ParameterizedTest'”。与相同@ValueSource:
码:
import org.junit.jupiter.api.*;
class SSTest {
@ParameterizedTest
@ValueSource(ints = {1, 2, 3})
void testSlowSort(int arg) {
}
@Test
void testSort() {
}
}
Run Code Online (Sandbox Code Playgroud)
PS:同样org.junit.jupiter.params缺少包装。否则,IntelliJ将自动导入它。
我希望任何人都可以帮助我解决此问题。我不使用Maven,Gradle等,而仅使用Java。
如果预期变量是整数,它就像这样
[DataRow(2)]
[TestMethod]
public void TestMethod(int expected)
{
// some code...
}
Run Code Online (Sandbox Code Playgroud)
但是当有二维数组 int[,] 而不是 int 参数时该怎么办呢?当我尝试这样做时
[DataRow(new int[,] { {0, 0}, {0, 0} })]
[TestMethod]
public void TestMethod(int[,] expected)
{
// some code...
}
Run Code Online (Sandbox Code Playgroud)
错误说
属性参数必须是属性参数类型的常量表达式、typeof 表达式或数组创建表达式
c# unit-testing mstest data-driven-tests parameterized-unit-test
我试图写它接受一个字符串作为一个对位计和抛出异常的方法的单元测试,如果它是畸形的(并且没有,如果它是好的).我想写一个参数化测试在几个字符串和期望的异常,其中饲料(包括是否良好形成的输入字符串没有抛出如此!).如果尝试使用@Test(expect = SomeException.class)注释,我遇到了两个问题:
expect = null是不允许的.那么我怎样才能测试抛出NO异常的预期结果(对于结构良好的输入字符串)?
期待=不可能?我还没有尝试过,但我强烈怀疑,这样的话看完这个(请你说明这是否是真的?): http://tech.groups.yahoo.com/group/junit/message/19383 这那么似乎是我发现的最佳解决方案.您如何看待它,特别是与此相比: 如何在参数化测试中测试异常?
提前感谢您的任何帮助,我期待着讨论:)
我正在尝试使用nose_parameterized测试并希望将其用于单元测试方法。
from nose.tools import assert_equal
from nose_parameterized import parameterized
import unittest
Class TestFoo(unittest.TestCase):
def setUp(self):
self.user1 = "Bar"
self.user2 = "Foo"
@parameterized.expand([
("testuser1",self.user1,"Bar"),
("testuser2",self.user2,"Foo")
]
def test_param(self,name,input,expected):
assert_equal(input,expected)
Run Code Online (Sandbox Code Playgroud)
但self在装饰器函数中没有定义。有解决方法吗?我知道我可以使用全局类变量,但我需要在setUp.
python nose nosetests parameterized-unit-test nose-parameterized
我有多种测试方法,应该测试多个参数的所有可能组合.我可以在这样的方法上使用NUnit ValueAttribute或RangeAttribute:
[TestFixture]
public class MyExampleTests
{
[Test]
public void TestedEntity_GivenParemeter_Passes(
[Values(1, 2)] int inputA,
[Range(1, 4)] int inputB)
{
if (inputA > 0 && inputB > 0)
Assert.Pass();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,在我的实际案例中,有4个参数,十几个方法和更多的值,因此写出每个方法的所有值变得冗长乏味,如果我想进行更改,我可能会在某处犯错.
如何将所有值组合的测试生成从各个方法移到TestFixture体中?以下不起作用,但它将是我想要的:
[TestFixture]
public class MyExampleTests2
{
readonly int inputA;
readonly int inputB;
public MyExampleTests2(
[Values(1, 2)] int inputA,
[Range(1, 4)] int inputB)
{
this.inputA = inputA;
this.inputB = inputB;
}
[Test]
public void TestedEntity_GivenParemeter_Passes()
{
if (this.inputA > 0 && this.inputB > …Run Code Online (Sandbox Code Playgroud) 我已经创建了一个单元测试项目。我得到一个异常指定
无法获取类*****。Tests.Controllers.PersonRegistration的默认构造函数
namespace *****.Tests.Controllers
{
[TestClass]
public class PersonRegistration
{
private ILoggingService _loggingService;
private IUserManager _userManager;
public PersonRegistration(IUserManager userManager, ILoggingService loggingService)
{
this._userManager = userManager;
this._loggingService = loggingService;
}
[TestMethod]
public void TestMethod1()
{
RegisterBindingModel model = new RegisterBindingModel();
AccountController ac = new AccountController(_userManager, _loggingService);
model.UserName = "test123@gmail.com";
var result = ac.Register(model);
Assert.AreEqual("User Registered Successfully", result);
}
}
}
Run Code Online (Sandbox Code Playgroud)
为了消除此问题,一些线程表示添加了不带参数的默认构造函数。所以我也加了
public PersonRegistration()
{
}
Run Code Online (Sandbox Code Playgroud)
但后来我解决了异常。但是我正在NULL为_userManager和获取价值
_loggingService
如何解决该问题。我不想在传递时生成空值。
请提出一种无需使用Moq或没有其他模拟框架的方法来解决此问题,以帮助我。
我试图传入一个数组来测试某个算法,但这些数组似乎没有正确传递或根本没有传递。我手动测试了算法,所以我知道它可以正常工作。如何传递数组以在 JUnit 5 中进行测试?
@ParameterizedTest
@CsvSource(value = {"[13,14,65,456,31,83],[1331,65456]"})
public void palindromeCombos(int[] input, int[] expected){
Palindrome pal = new Palindrome();
List<Integer> actual = pal.allPalindromes(input);
int[] result = new int[actual.size()];
for(int i = 0; i < actual.size(); i++){
result[i] = actual.get(i);
}
Assertions.assertArrayEquals(expected, result);
}
Run Code Online (Sandbox Code Playgroud) 如何在JUnit中使用参数化测试来测试以下方法
public class Math {
public static int add(int a, int b) {
return a + b;
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道如何使用Junit进行参数化测试以测试此方法,当我想用10个不同的args进行测试时.
我想用三个参数来参数我JUnit5测试:string,string和list<string>。
到目前为止,使用时没有运气@CsvSource,这是为我的用例传递参数的最便捷方法:
没有隐式转换将类型java.lang.String的对象转换为类型java.util.List
实际测试是:
@ParameterizedTest()
@CsvSource(
"2,1"
)
fun shouldGetDataBit(first: Int, second: String, third: List<String>) {
...
}
Run Code Online (Sandbox Code Playgroud)
知道这是否可行吗?我在这里使用Kotlin,但这应该无关紧要。