获取运行时case的方法名

  1.想要在case运行时,获取当前运行的类名和方法名可以通过如下代码实现

this.getClass().getSimpleName() + ":" + new Exception().getStackTrace()[1].getMethodName()

2.如果写在base文件里,每次运行时获取一遍接口名,获取不到方法名。所以这里通过反射来获取,具体如下

    @AfterMethod
    public void getMethodName(Method method){
        String currentMethodName = method.getName();
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pytest 内置了许多 fixture,可以帮助我们在测试用例中获取测试状态、共享测试状态等。下面是常用的内置 fixture 的用法: 1. request request fixture 可以用来获取测试用例的信息,如测试用例的称、参数等。示例代码如下: ```python def test_case(request): print("当前运行的测试用例称:", request.node.name) ``` 2. tmpdir tmpdir fixture 可以用来在测试用例中创建临时文件或目录,可以方便地进行文件操作。示例代码如下: ```python def test_case(tmpdir): file_path = tmpdir.join('test.txt') with open(file_path, 'w') as f: f.write('hello world') assert file_path.read() == 'hello world' ``` 3. capsys capsys fixture 可以用来获取测试用例中的输信息,可以方便地进行输断言。示例代码如下: ```python def test_case(capsys): print("hello world") captured = capsys.readouterr() assert captured.out == "hello world\n" ``` 4. monkeypatch monkeypatch fixture 可以用来在测试用例中修改代码的行为,可以方便地进行代码覆盖。示例代码如下: ```python def test_case(monkeypatch): def mock_func(): return "mocked result" monkeypatch.setattr('module.func', mock_func) assert module.func() == "mocked result" ``` 5. pytestconfig pytestconfig fixture 可以用来获取 pytest 的配置信息,可以方便地进行配置管理。示例代码如下: ```python def test_case(pytestconfig): print("pytest 配置信息:", pytestconfig.option) ``` 除了以上常用的内置 fixture,pytest 还提供了很多其他的 fixture,可以满足不同的测试需求。另外,通过 fixture 可以在测试用例之间共享状态,例如在 conftest.py 文件中定义一个共享的 fixture,可以让所有测试用例都可以使用这个 fixture。示例代码如下: ```python # conftest.py 文件中定义共享的 fixture @pytest.fixture(scope="session") def shared_data(): return {"data": []} # 测试用例中使用共享的 fixture def test_case(shared_data): shared_data["data"].append("test data") assert len(shared_data["data"]) == 1 def test_case2(shared_data): assert len(shared_data["data"]) == 1 ``` 在上面的示例中,我们在 conftest.py 文件中定义了一个共享的 fixture shared_data,它是一个字典,用来存储测试用例的共享数据。在测试用例中,我们可以通过参数 shared_data 来使用这个 fixture,然后在测试用例中操作 shared_data,例如向其中添加一个元素。在另一个测试用例中,我们可以再次使用 shared_data,验证它是否包含了我们添加的元素。这样可以方便地在测试用例之间传递状态,避免了重复编写代码的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值