@pytest.mark.parametrize('keyword',[ 'balck', 'avis' ]) def test_search_none(self,keyword): msg = self.owner_page.search_none(keyword) assert 'No owners' in msg
时间: 2024-02-06 07:03:00 浏览: 25
你的测试用例现在看起来应该像这样:
```
@pytest.mark.parametrize('keyword',[
'balck',
'avis'
])
def test_search_none(self, keyword):
msg = self.owner_page.search_none(keyword)
assert 'No owners' in msg
```
这个测试用例使用了 `@pytest.mark.parametrize` 装饰器,以便在两个不同的搜索关键字上运行相同的测试。它还使用了 `assert` 语句来检查返回的消息是否包含字符串 "No owners"。如果找不到任何所有者,则该测试应该通过,否则将引发 AssertionError。
相关问题
@pytest.mark.parametrize 或者 @pytest.mark.allure.step用法
`@pytest.mark.parametrize` 是 pytest 提供的一个装饰器,可以用于参数化测试用例。它可以让我们在测试用例中使用多组参数来执行同一个测试用例,从而减少测试用例的编写量,提高效率。
示例代码:
```python
import pytest
@pytest.mark.parametrize("input, expected_output", [
(1, 2),
(2, 3),
(3, 4),
(4, 5),
])
def test_increment(input, expected_output):
assert input+1 == expected_output
```
`@pytest.mark.allure.step` 也是 pytest 提供的一个装饰器,用于生成测试报告。它可以将测试用例中的每个步骤作为报告的一个单独的步骤展示,从而更直观地展示测试用例的执行情况。
示例代码:
```python
import pytest
import allure
@allure.step("输入用户名和密码")
def input_username_and_password(username, password):
pass
@allure.step("点击登录按钮")
def click_login_button():
pass
@allure.step("验证登录成功")
def verify_login_success():
pass
def test_login():
input_username_and_password("testuser", "testpass")
click_login_button()
verify_login_success()
```
以上是 `@pytest.mark.parametrize` 和 `@pytest.mark.allure.step` 的基本用法。需要注意的是,`@pytest.mark.allure.step` 需要安装 `pytest-allure-adaptor` 才能正常使用。
test_onwer_search.py:35 (TestOwnerSearch.test_search_none[avis]) self = <tests.petclinic.test_owner.test_onwer_search.TestOwnerSearch object at 0x00000167CA97D2D0> keyword = 'avis' @pytest.mark.parametrize('keyword',[ 'balck', 'avis' ]) def test_search_none(self,keyword): msg = self.owner_page.search_none(keyword) > assert 'No owners' in msg E TypeError: argument of type 'NoneType' is not iterable test_onwer_search.py:42: TypeError
这个错误意味着在第35行中的 `self.owner_page.search_none(keyword)` 返回了 None,而不是一个可以迭代的对象。因此,在第42行中的 `'No owners' in msg` 中引发了 TypeError。你需要检查 `search_none` 方法是否正确地返回了一个字符串,或者在测试用例中添加必要的检查以避免这种情况。
如果 `search_none` 方法返回 None,则你需要检查该方法的实现并确保它始终返回一个字符串。你可以尝试在该方法的末尾添加以下代码:
```
def search_none(self, lastname) -> str:
# your existing code here
if not owners:
return 'No owners'
# any other existing code here
return ''
```
这样,如果无论什么原因导致该方法没有返回字符串,它也不会返回 None,并且测试用例将不会引发 TypeError。
或者,你可以在测试用例中添加一个检查以避免 TypeError:
```
@pytest.mark.parametrize('keyword',[
'balck',
'avis'
])
def test_search_none(self, keyword):
msg = self.owner_page.search_none(keyword)
if msg is not None:
assert 'No owners' in msg
```
这个测试用例添加了一个条件语句,用于检查返回的消息是否为 None。如果是 None,则测试用例将直接通过。否则,它将检查消息是否包含字符串 "No owners"。这样,即使 `search_none` 方法返回 None,测试用例也不会引发 TypeError。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)