我是pytest的新手,正在努力通过它。 我目前正在编写一个小型的CLI游戏,它将连续需要多个用户输入,但我不知道该怎么做。 我读了很多解决方案,但没有设法使它起作用。
这是我的代码:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import InputBase from '@material-ui/core/InputBase';
import { withStyles } from '@material-ui/core/styles';
import { withRouter } from 'react-router-dom';
import { connect } from 'react-redux';
import { getAppInfo } from '../../actions/appActions.js';
import constants from '../../constants.js';
import { AppSearchBarInputStyles } from '../styles/Material-UI/muiStyles.js';
import AppNotFound from './AppNotFound.js';
import * as log from 'loglevel';
log.setLevel("debug")
class AppSearchBarInput extends Component {
state = {
appId: '',
snackBarOpen: false
}
onChange = e => {
this.setState({ appId: e.target.value });
}
onKeyDown = e => {
const { appId } = this.state;
const { found } = this.props.app;
if (e.keyCode === constants.ENTER_KEY) {
this.props.getAppInfo({ appId });
if (found) {
this.props.history.push('/moreInfo');
} else {
this.setState({
snackBarOpen: true
});
}
this.setState({
appId: ''
});
}
}
handleCloseSnackBar = () => {
this.setState({
snackBarOpen: false
});
}
render() {
const { classes, app } = this.props;
const { snackBarOpen } = this.state;
const { found, appId } = app;
let message = '';
if (!found) {
message = appId === '' ? constants.MESSAGES.APP_BLANK() : constants.MESSAGES.APP_NOT_FOUND(appId);
}
let displayWhenFoundIsUndefined = null;
if (found === undefined) {
displayWhenFoundIsUndefined = <div>Loading...</div>;
} else {
displayWhenFoundIsUndefined = <AppNotFound message={message}
open={!found}
onClose={this.handleCloseSnackBar}/>;
}
return (
<div>
<InputBase
placeholder="Search…"
classes={{
root: classes.inputRoot,
input: classes.inputInput,
}}
onChange={this.onChange}
onKeyDown={this.onKeyDown}
value={this.state.appId} />
{displayWhenFoundIsUndefined}
</div>
)
}
}
AppSearchBarInput.propTypes = {
classes: PropTypes.object.isRequired
}
const mapStateToProps = state => ({
app: state.app.app
});
const AppSearchBarWithStyles = withStyles(AppSearchBarInputStyles)(AppSearchBarInput);
const AppSearchBarWithStylesConnected = connect(mapStateToProps, { getAppInfo })(AppSearchBarWithStyles);
export default withRouter(AppSearchBarWithStylesConnected);
我只是想自动化这两个请求的用户输入。
(即:将class Player:
def __init__(self):
self.set_player_name()
self.set_player_funds()
def set_player_name(self):
self.name = str(input("Player, what's you name?\n"))
def set_player_funds(self):
self.funds = int(input("How much money do you want?\n"))
和input "Bob"
有人可以帮忙吗? 谢谢!
答案 0 :(得分:0)
测试输入的相当优雅的方法是使用 monkeypatch
固定装置模拟输入文本。可以使用 lambda
语句和迭代器对象来处理多个输入。
def test_set_player_name(monkeypatch):
# provided inputs
name = 'Tranberd'
funds = 100
# creating iterator object
answers = iter([name, str(funds)])
# using lambda statement for mocking
monkeypatch.setattr('builtins.input', lambda name: next(answers))
player = Player()
assert player.name == name
assert player.funds == funds
monkeypatch
在 docs.pytest.org。
wiki.python.org 上的 Iterator object
。