小编xia*_*x48的帖子

如何标记一个函数而不是pytest的测试?

我正在使用 pytest 来测试一些基于 TensorFlow 的代码。

ATestCase的定义是为了简单起见,例如:

class TestCase(tf.test.TestCase):
    # ...
Run Code Online (Sandbox Code Playgroud)

问题是tf.test.TestCase提供了一个有用的函数self.test_session(),由于它的名字以test_.

结果 pytest 报告了比我由于test_session()方法定义的测试方法更多的成功测试。

我使用以下代码跳过test_session

class TestCase(tf.test.TestCase):
    @pytest.mark.skip
    @contextmanager
    def test_session(self):
        with super().test_session() as sess:
            yield sess
Run Code Online (Sandbox Code Playgroud)

然而,测试报告中会有一些“s”表示有一些跳过测试。

无论如何,我可以在不全局更改 pytest 测试发现规则的情况下标记一种确切的方法而不是测试方法吗?

python unit-testing pytest tensorflow

7
推荐指数
1
解决办法
2075
查看次数

如何在 TypeScript 中定义一种类型,该类型可以具有除特定属性之外的任何属性?

当编写像商店这样的字典的接口时,我想区分数据模型和商店中的项目,即id和模型。我想添加约束,模型本身在其界面中不使用字段id,但我不知道该怎么做。

type Item<T> = T & {id: string} 
// T is type of model
// Item<T> is type of objects in the store, use id as primary key
Run Code Online (Sandbox Code Playgroud)

以下函数是简化版,用于在商店中添加新商品

function add<T>(model: Item<T>): T {
    const id = uuid();
    const result = {...model, id};
    store.push(result);
    return result;
}
Run Code Online (Sandbox Code Playgroud)

最好在没有属性的T情况下添加一些约束,否则会与 相同,同时in与 中的不一样,这会导致错误。TidTItem<T>idItem<T>T

作为总结,我需要这样的类型

type Item<T extends "The type which allow any type without property id: string"> = T & {id : …
Run Code Online (Sandbox Code Playgroud)

typescript

5
推荐指数
1
解决办法
1764
查看次数

是否可以在不使用 JSX 的情况下使用 React Hooks API(在打字稿中)

当将 JSX 版本的 React Hooks API演示翻译成没有 JSX 的版本时,遵循React-without-jsx,我得到了以下代码

import React, { useState } from 'react';
import ReactDOM from 'react-dom';

export function test() {
  ReactDOM.render(Count(), document.getElementById('main'));
}

export function Count() {
  const e = React.createElement;
  const [count, setCount] = useState(0);
  const button = e('button', {
    onClick: () => {
      setCount(count + 1);
    },
  });
  return e('div', null, e('p', `You clicked ${count} times`), button);
}
Run Code Online (Sandbox Code Playgroud)

但是,当在浏览器中运行此代码时,出现以下错误

react.development.js:1551 Uncaught Error: Invalid hook call. Hooks can only be called inside …
Run Code Online (Sandbox Code Playgroud)

jsx typescript reactjs react-hooks

2
推荐指数
1
解决办法
3188
查看次数