我在conftest.py中有一个会话范围的夹具
@pytest.fixture(scope="session",autouse=True)
def log(request):
testlog = LogUtil(testconf.LOG_NAME).get()
return testlog
Run Code Online (Sandbox Code Playgroud)
当mytest.py中定义了测试方法时,这将加载并按预期工作,如下所示:
def test_hello_world(self, log):
log.info("hello world test")
Run Code Online (Sandbox Code Playgroud)
有没有办法使用夹具(因为它启用了autouse),而无需在测试方法中添加额外的"log"参数?
def test_hello_world(self):
log.info("hello world test") #log is not found
@pytest.mark.usefixtures("log")
def test_hello_world2(self):
log.info("hello world test") #log is not found
def test_hello_world3(self,log):
log.info("hello world test") #log object is accessible here
Run Code Online (Sandbox Code Playgroud)
错误 - NameError:未定义名称"log"
我正在尝试通过 xml 使用 Spring Boot 应用程序连接到两个不同的rabbitmq 集群并使用它们。当在应用程序上下文中创建单个 rabbit:connection-factory bean 时,它运行良好。但是,当添加第二个时,它无法启动应用程序,并显示错误“org.springframework.boot.autoconfigure.amqp.RabbitAnnotationDrivenConfiguration 中的方法 rabbitListenerContainerFactory 的参数 1 需要一个 bean,但找到了 2 个:”。如何为每个集群创建不同的工厂?如果这不是正确的方法,请提出一种替代方法?
这是xml片段:
<rabbit:connection-factory id="firstConnectionFactory" connection-factory="firstSpringConnectionFactory" />
<rabbit:connection-factory id="secondConnectionFactory" connection-factory="secondSpringConnectionFactory"/>
<bean id="firstSpringConnectionFactory"
class="org.springframework.amqp.rabbit.connection.RabbitConnectionFactoryBean">
<property name="useSSL" value="${rabbitmq.ssl.enabled}" />
<property name="host" value="${rabbitmq.first.host}"/>
<property name="virtualHost" value="${rabbitmq.vhost}"/>
<property name="port" value="${rabbitmq.cluster.port}"/>
<property name="username" value="${rabbitmq.user}"/>
<property name="password" value="${rabbitmq.first.password}"/>
</bean>
<bean id="secondSpringConnectionFactory"
class="org.springframework.amqp.rabbit.connection.RabbitConnectionFactoryBean">
<property name="useSSL" value="${rabbitmq.ssl.enabled}" />
<property name="host" value="${rabbitmq.second.host}"/>
<property name="virtualHost" value="${rabbitmq.vhost}"/>
<property name="port" value="${rabbitmq.cluster.port}"/>
<property name="username" value="${rabbitmq.user}"/>
<property name="password" value="${rabbitmq.second.password}"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
和侦听器容器代码:
ConnectionFactory cf = rabbitConnectionFactory;//One of …Run Code Online (Sandbox Code Playgroud)