如何解决在 Pytest 中相互测试两个类
我刚刚开始学习使用 assert bar.bar(foo.foo('hello')) == 'hellofoobar'
编写单元测试,但我找不到任何关于测试以检查两个类是否一起正常工作的内容,其中一个类获取第二个类的输出,例如
test_foo.py
pytest 是否应该用于运行涉及 2 个类的此类测试?如果是这样,这个测试应该用 test_bar.py
还是 class Foo:
def foo(self,x):
return f'{x}foo'
编写?
这样的测试会被称为集成测试吗?
Foo.py
class Bar:
def bar(self,x):
return f'{x}bar'
Bar.py
import pytest
from .Foo import Foo
from .Bar import Bar
@pytest.fixture
def foo():
return Foo()
@pytest.fixture
def bar():
return Bar()
conftest.py
def test_foo(foo):
assert foo.foo('hello') == 'hellofoo'
test_foo.py
def test_bar(bar):
assert bar.bar('world') == 'worldbar'
test_bar.py
__init__.py
<form>
<label for ="room">Select a room:</label>
<select name="room" id="room">
<?php
$args = array(
'post_type' => 'room','posts_per_page' => -1,'orderby' => 'title','order' => 'ASC'
);
$the_query = new WP_Query($args);
$sobe = get_posts($args);
foreach ($sobe as $post){
setup_postdata($post);
$room_title = get_field('room_name');
?>
<option value=" <?php echo $room_title ?> " > <?php echo $room_title ?> </option>
<?php
}
wp_reset_postdata();
?>
</select>
</br>
</br>
</form>
解决方法
我认为这完全没问题。它期望 Foo
和 Bar
都能正常工作。在这种情况下,我认为您可以将其称为集成测试。
test_bar.py
def test_bar_with_foo(bar,foo):
assert bar.bar(foo.foo('hello')) == 'hellofoobar'
是否需要此类测试可能取决于每个类的复杂程度。在上述情况下(非常简单),我认为打破测试是可以的;意思是如果 test_bar.test_bar
通过而 test_foo.test_foo
通过则 test_bar.test_bar_with_foo
变得多余。
如果我们分解测试并假设 Foo
返回正确的内容,则测试开始如下所示。在这一点上出现了问题;此测试是否提供了超出 test_bar.test_bar
的其他价值?
test_bar.py
def test_bar_2(bar):
assert bar.bar('hellofoo') == 'hellofoobar'
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。