标签: parameterized-tests

茉莉参数化单元测试

好吧作为C#NUnit的家伙,这可能很奇怪.

但茉莉花是否允许参数化单元测试?

我不确定它是否违反"声明"和"它"以使非程序员可读.

我见过一些第三方插件,但它们有点陈旧,不确定它是否已添加到茉莉花中.如果我想使用插件

只是为了帮助将来发现这一点的人,我在茉莉花论坛上被告知在Jasmine本身内没有对参数化测试的一流支持.

unit-testing parameterized-unit-test jasmine parameterized-tests

24
推荐指数
4
解决办法
7604
查看次数

使用JUnit @Rule使用Mockito进行参数化测试?

这是从这个问题开始的:我被要求开始一个新问题.

问题是我对JUnit不够了解Rule,或者在这里发生了什么Runners等等,以Jeff Bowman提到的方式解决问题.

java junit rule mockito parameterized-tests

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

如何在 JUnit 5 中使用少量枚举创建参数化测试?

例如,我的项目中有几个枚举:

  • 图,值为 TRIANGLE 和 SQUARE
  • 颜色,值为红色和黄色

如何使用所有组合的笛卡尔积创建测试?以下代码不起作用:

// this doesn't work
@ParameterizedTest
@EnumSource(value = Figure.class)
@EnumSource(value = Color.class)
void test(Figure figure, Color color) {
    System.out.println(String.format("%s_%s",figure,color));
}
Run Code Online (Sandbox Code Playgroud)

我想获得所有组合:

TRIANGLE RED
TRIANGLE YELLOW
SQUARE RED
SQUARE YELLOW
Run Code Online (Sandbox Code Playgroud)

我的临时解决方案是使用注释@MethodSource

// this works
@ParameterizedTest
@MethodSource("generateCartesianProduct")
void test(Figure figure, Color color) {
    System.out.println(String.format("%s_%s",figure,color));
}


private static Stream<Arguments> generateCartesianProduct() {
    List<Arguments> argumentsList = new ArrayList<>();

    for(Figure figure : Figure.values()) {
        for(Color color : Color.values()) {
            argumentsList.add(Arguments.of(figure,color));
        }
    }

    return argumentsList.stream();
}
Run Code Online (Sandbox Code Playgroud)

但我不想在测试中添加额外的代码。JUnit 5 有解决我的问题的方法吗?

java junit unit-testing parameterized-tests junit5

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

NotAMockException /如何在Kotlin中参数化测试中存根值?

对于以下Kotlin类:

class ProductLogic(

        private val product: Product?

) {

    fun shouldShow(): Boolean {
        if (product == null) {
            return false
        }
        val version = product.version!!
        if (!Utils.isAtLeastVersionX(version.major, version.minor)) {
            return false
        }
        return true
    }

}
Run Code Online (Sandbox Code Playgroud)

我想在Kotlin写一个参数化测试:

@RunWith(ParameterizedRobolectricTestRunner::class)
@Config(constants = BuildConfig::class, sdk = [19], packageName = "com.example")
class ProductLogicTest(

        private val product: Product?,
        private val shouldShow: Boolean

) {

    @Before
    fun setUp() {
        // doReturn(VERSION).`when`(product).version // (2) Raises a NotAMockException
    }

    @Test
    fun shouldShow() {
        assertThat(ProductLogic(product).shouldShow(), `is`(shouldShow))
    }

    companion object …
Run Code Online (Sandbox Code Playgroud)

junit mockito robolectric kotlin parameterized-tests

8
推荐指数
1
解决办法
374
查看次数

Eclipse中的参数化Groovy JUnit测试用例

Parameterized在Eclipse中运行Groovy JUnit测试用例时遇到问题(请参阅下面的测试代码和环境详细信息).

症状

  • 右键单击Package Explorer中的类并执行Run As - > JUnit Test Case,只会弹出一个声明"找不到JUnit测试"的对话框.
  • 右键单击项目并执行Run As - > JUnit Test Case运行参数化Groovy 之外的所有测试用例.

我尝试过的事情

  1. 确保运行"正常"Groovy JUnit测试用例. 这有效.
  2. 确保运行参数化Java测试用例. 这有效.
  3. 为此测试用例手动创建JUnit运行配置. 这有效.

所以

所以我有一个不方便的解决方法(3).但这不可扩展,因为当我在项目中运行所有测试用例时,仍然不会包含此测试用例.

任何想法如何让Eclipse/Groovy插件/ JUnit自动识别我的测试用例?


测试用例代码

import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.junit.runners.Parameterized.Parameters

@RunWith(Parameterized)
public class TestParams {
    final int a

    public TestParams(int a) { this.a = a }

    @Parameters
    public static Collection<Object[]> data() {
        def cases = new Object[2][1]
        cases[0][0] = 3
        cases[1][0] …
Run Code Online (Sandbox Code Playgroud)

eclipse groovy junit parameterized-tests groovy-eclipse

7
推荐指数
1
解决办法
1823
查看次数

f#中的参数化测试 - 这不是有效的常量表达式

出于某种原因,当通过TestCaseattrubute 将参数传递给测试时,我得到以下关于第一个参数的错误消息,在本例中是一个数组:

这不是有效的常量表达式或自定义属性值

module GameLogicTest = 
    open FsUnit
    open NUnit.Framework
    open GameLogic.Examle

    // This is not a valid constant expression or custom attribute value
    [<TestCase( [| 1; 2; 3 |], 3, 1,1)>]
    let ``let example.`` (a, m, h, c) = 
        a
        |> proof1 m
        |> should equal (h,c)
Run Code Online (Sandbox Code Playgroud)

但是当从属性和方法本身中删除最后一个参数时,一切正常.

[<TestCase( [| 1; 2; 3 |], 3, 1)>]
let ``let example.`` (a, m, h) = 
    a
    |> proof1 m
    |> should equal (h,1)
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?我最好也定义一个元组int * int但它似乎也不起作用.

f# custom-attributes constant-expression fsunit parameterized-tests

5
推荐指数
1
解决办法
828
查看次数

使用参数化测试返回方法应该没有参数

我有一个问题,我想通过测试方法来使用GET方法和POST发送请求。我使用了参数化,但我得到信息 java.lang.Exception: Method simpleMessage should have no参量

  @Test
    @ParameterizedTest
    @ValueSource(strings = {"true", "false"})
    public void simpleMessage (boolean isPost) {
        verifyIdOdpEqual(isPost,1243, "message");
    }
Run Code Online (Sandbox Code Playgroud)

java junit parameterized-tests

5
推荐指数
1
解决办法
2万
查看次数

如何参数化 Spring Boot 测试的属性值

我正在使用 spring boot 开发一个聚合框架,该框架可用于聚合多种 I/O 操作的结果。此外,它还支持非阻塞(反应式)和阻塞聚合层,该聚合层由属性控制,该属性又用于条件 bean。对于集成测试,我一直在使用此设置:

abstract class HttpUpstreamTests {
    // Setup & test logic
}

@SpringBootTest(properties = {
        "aggregation.reactive.enabled=false",
})
class BlockingAggregationIT extends HttpUpstreamTests {}

@SpringBootTest(properties = {
        "aggregation.reactive.enabled=true",
})
class NonBlockingAggregationIT extends HttpUpstreamTests {}
Run Code Online (Sandbox Code Playgroud)

现在我必须添加几个基类:

abstract class MongodbUpstreamTests {
    // Setup & test logic
}
abstract class SqlUpstreamTests {
    // Setup & test logic
}
Run Code Online (Sandbox Code Playgroud)

但当然,我不能让 IT 类从多个抽象类扩展。一种方法是使所有 upstrteam 测试参数化测试,但我似乎找不到一种方法来使用该方法与@SpringBootTest(properties="aggregation.reactive.enabled=false"). 有没有办法使用不同的属性作为参数化测试的参数?

java testing parameterized-tests spring-boot spring-boot-test

5
推荐指数
0
解决办法
571
查看次数

有没有更快的方法为Django视图编写类似的测试用例?

基本上,我意识到我正在test_update_with_only_1_field为多个模型的相似网址编写相同的测试用例()

from django.test import RequestFactory, TestCase
class BaseApiTest(TestCase):
def setUp(self):
    superuser = User.objects.create_superuser('test', 'test@api.com', 'testpassword')
    self.factory = RequestFactory()
    self.user = superuser
    self.client.login(username=superuser.username, password='testpassword')

class SomeModelApiTests(base_tests.BaseApiTest):
def test_update_with_only_1_field(self):
    """
    Tests for update only 1 field 

    GIVEN the following shape and related are valid
    WHEN we update only with just 1 field
    THEN we expect the update to be successful
    """
    shape_data = {
        'name': 'test shape',
        'name_en': 'test shape en',
        'name_zh_hans': 'test shape zh hans',
        'serial_number': 'test shape serial …
Run Code Online (Sandbox Code Playgroud)

python django python-unittest parameterized-tests

4
推荐指数
1
解决办法
528
查看次数

在TestNG中参数化@BeforeMethod方法

我的测试有一个基础测试类,该类在每次测试之前都会进行初始化工作。

这是代码

public class BaseTestParameters {

  MyObj myObj;

  @DataProvider(name = "apiType")   
  public static Object[][] createData() {

     return new Object[][] {{"type", "1"},{"type","2"}};   
  } 

  @BeforeMethod()   
  @Factory(dataProvider = "apiType")   
  public void setup(String type,String param) throws Exception {

     myObj = createMyObject(param);

  }
}
Run Code Online (Sandbox Code Playgroud)

我所有的测试类都扩展了该基类,并使用myObj进行测试。

myObj有两种不同的创建方式(取决于参数)。所有测试将运行两次。每种构成的一种myObj

如何启用此方案?使用@Factory注释意味着我需要Object[]从该方法中返回,但是我不必从该方法中返回任何测试类。

java testng parameterized-tests testng-dataprovider

4
推荐指数
1
解决办法
183
查看次数