JUnit5 的 Tag、Filter、Order、Lifecycle

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 Tag JUnit5 可以使用@Tag注解给测试类和测试方法打 tag,这些 tag 能用来在执行时进行过滤,它跟 group 有点类似。 tag 应该遵循以下规则: 不能为 null 或者为空。 不能包含空格。 不能包含 ISO 控制字符。 不能包含保留字符:, ( ) & | ! 示例代码: import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @Tag("fast") @Tag("model") class TaggingDemo { @Test @Tag("taxes") void testingTaxCalculation() { } } @Tag还可以通过元注解和组合注解,实现自定义注解,参考: https://dongfanger.gitee.io/blog/JUnit/002-JUnit5%E6%B3%A8%E8%A7%A3%E5%AD%A6%E4%B9%A0%E6%8C%87%E5%BC%95.html#id2 Filter 打好了 tag 后,在执行时可以进行过滤,比如 Maven 配置: <!-- ... --> <build> <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> <configuration> <groups>acceptance | !feature-a</groups> <excludedGroups>integration, regression</excludedGroups> </configuration> </plugin> </plugins> </build> <!-- ... --> groups 用来指定包含哪些 tag,excludedGroups 用来指定排除哪些 tag。 ...

pytest 内置和自定义 marker

可以通过命令行查看所有 marker,包括内置和自定义的

send_keys 报错 element_not_interactable

这两天要在阿里云日志中操作 UI,以输入关键字搜索日志。

SpringBoot 接入 JaCoCo 实践教程

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 静态接入和动态接入 SpingBoot 可以通过 2 种方式接入 JaCoCo:Maven 和 Agent。Maven 方式是静态接入,在编译时计算代码覆盖率。Agent 方式是动态接入,服务启起来以后,能实时根据代码命中情况计算代码覆盖率。 教程代码已上传开源项目:https://github.com/dongfanger/mini Maven pom.xml 添加依赖: <dependency> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.7</version> </dependency> 添加配置: <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${project.parent.version}</version> </plugin> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.7</version> <executions> <execution> <id>default-prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>default-report</id> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin> </plugins> </build> 点击 maven 的 install: target/site/jacoco目录下就生成了覆盖率报告: Agent 下载 jacoco 文件后解压,提取 2 个文件: 使用命令启动 SpringBoot,启动参数添加-javaagent: java -javaagent:jacoco/jacocoagent.jar -jar target/mini-0.0.1-SNAPSHOT.jar 此时会生成 1 个jacoco.exec文件,这个文件就是覆盖率数据文件,采用以下命令根据数据文件生成 HTML 报告: ...

把 RPC 包进事务的暗坑

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 # 方式:把判断放进 SQL,一条语句的原子性 rows = db.execute(""" UPDATE pay_record SET status = 'PAID' WHERE order_no = ? AND status = 'INIT' """, order_no) if rows == 0: return "已处理" # 别人改过了,我不是第一个 grant_coupon(order_no) # 只有改成功的那个才发券 return "success" grant_coupon 失败会发生什么? 券永久丢失,该发没发,资损。 第一次请求: UPDATE status: INIT → PAID ✅ 已提交,改不回去了 grant_coupon(order_no) ❌ 抛异常 / 超时 / 下游挂了 第二次请求: rows = db.execute("UPDATE ... WHERE status = 'INIT'") # status已是PAID # rows == 0 return "已处理" # ← 被自己的幂等逻辑拦住,不会执行grant_coupon 包进一个事务能解决吗 把 update 和 grant_coupon 这两个操作,包进一个事务里,要么同时执行,要么同时回滚,是不是就解决了? ...

HttpRunner3 的用例是怎么运行起来的

在 PyCharm 中打开examples/httpbin/basic_test.py:

JMeter 英文版界面介绍

JMeter 的界面虽然不华丽,但是使用起来还是很便利。本文将基于英文版界面对整个可视区域进行介绍,如果想看中文版,请在菜单栏点击 Choose Language 切换:

JUnit5 编写基本测试

JUnit5 的测试不是通过名称,而是通过注解来标识的。

pytest 精通 fixture

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 fixture 是什么 看看源码 def fixture( callable_or_scope=None, *args, scope="function", params=None, autouse=False, ids=None, name=None ): """Decorator to mark a fixture factory function. This decorator can be used, with or without parameters, to define a fixture function. The name of the fixture function can later be referenced to cause its invocation ahead of running tests: test modules or classes can use the ``pytest.mark.usefixtures(fixturename)`` marker. Test functions can directly use fixture names as input arguments in which case the fixture instance returned from the fixture function will be injected. Fixtures can provide their values to test functions using ``return`` or ``yield`` statements. When using ``yield`` the code block after the ``yield`` statement is executed as teardown code regardless of the test outcome, and must yield exactly once. :arg scope: the scope for which this fixture is shared, one of ``"function"`` (default), ``"class"``, ``"module"``, ``"package"`` or ``"session"`` (``"package"`` is considered **experimental** at this time). This parameter may also be a callable which receives ``(fixture_name, config)`` as parameters, and must return a ``str`` with one of the values mentioned above. See :ref:`dynamic scope` in the docs for more information. :arg params: an optional list of parameters which will cause multiple invocations of the fixture function and all of the tests using it. The current parameter is available in ``request.param``. :arg autouse: if True, the fixture func is activated for all tests that can see it. If False (the default) then an explicit reference is needed to activate the fixture. :arg ids: list of string ids each corresponding to the params so that they are part of the test id. If no ids are provided they will be generated automatically from the params. :arg name: the name of the fixture. This defaults to the name of the decorated function. If a fixture is used in the same module in which it is defined, the function name of the fixture will be shadowed by the function arg that requests the fixture; one way to resolve this is to name the decorated function ``fixture_<fixturename>`` and then use ``@pytest.fixture(name='<fixturename>')``. """ if params is not None: params = list(params) fixture_function, arguments = _parse_fixture_args( callable_or_scope, *args, scope=scope, params=params, autouse=autouse, ids=ids, name=name, ) scope = arguments.get("scope") params = arguments.get("params") autouse = arguments.get("autouse") ids = arguments.get("ids") name = arguments.get("name") if fixture_function and params is None and autouse is False: # direct decoration return FixtureFunctionMarker(scope, params, autouse, name=name)( fixture_function ) return FixtureFunctionMarker(scope, params, autouse, ids=ids, name=name) 总结一下 ...

webdriver 使用已打开过的 chrome

基本功能: