小编Sal*_*dec的帖子

使用 RSpec 用 get 测试用户输入

我是使用 RSpec 和 Ruby 进行单元测试的新手,我有一个关于如何测试我的代码是否使用该gets方法但不提示用户输入的问题。

这是我要测试的代码。这里没什么疯狂的,只是一个简单的一个班轮。

我的文件.rb

My_name = gets
Run Code Online (Sandbox Code Playgroud)

这是我的规格。

require 'stringio'

def capture_name
    $stdin.gets.chomp
end

describe 'capture_name' do
    before do
        $stdin = StringIO.new("John Doe\n")
    end

    after do 
        $stdin = STDIN
    end

    it "should be 'John Doe'" do 
        expect(capture_name).to be == 'John Doe'
        require_relative 'my_file.rb'
    end
end
Run Code Online (Sandbox Code Playgroud)

现在这个规范有效,但是当我运行代码时,它会提示用户输入。我不希望它这样做。我想简单地测试是否正在调用获取方法并可能模拟用户输入。不确定如何在 RSpec 中实现这一点。在 Python 中,我会使用unittest.mock在 RSpec 中是否有类似的方法?

提前致谢!

ruby unit-testing rspec

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

AttributeError: None 没有属性“打印”

所以我正在练习一些单元测试,我正在尝试检查 For 循环内的输出。这是我的运行代码

def main():

  for i in range(100):
    print("Argh!")
Run Code Online (Sandbox Code Playgroud)

非常基本,现在这是我的测试代码。

import unittest
from unittest import mock  # possibly "from unittest import mock" depending on version.
from RunFile import main

class TestMain(unittest.TestCase):
    def test_main(self):
        with mock.patch.object(main(), 'print') as mock_print:
            main()
        expected_calls = [mock.call('Argh!') for _ in range(100)]
        mock_print.assert_has_calls(expected_calls)

if __name__ == '__main__':
    unittest.main()
Run Code Online (Sandbox Code Playgroud)

这是我返回的错误消息。我不确定如何解决这个问题。更新:这是完整的追溯

======================================================================
ERROR: test_main (__main__.TestMain)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:/Users/jsalce/Documents/Testsuites/IfStatements/Testsuite.py", line 9, in test_main
    with mock.patch.object(RunFile, 'print') as mock_print:
  File "C:\Python33\lib\unittest\mock.py", line 1148, in …
Run Code Online (Sandbox Code Playgroud)

python unit-testing function

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

检查函数中的值单元测试

我想知道如何创建单元测试来检查函数内变量的值?我有下面的代码,仅当变量是我得到的全局变量时才有效,但这不是我想要做的。我只想测试变量在主函数中的值。我该怎么做呢?这是我的代码:

运行代码

def main():
    #Declare Variable Here
    Gold = 4000

def treasure_chest(amount):
    print("I have ",amount," Gold!")
Run Code Online (Sandbox Code Playgroud)

这是我的测试套件

from Functions import main
import unittest

class MyTestCase(unittest.TestCase):
    def test_valueContains(self):
        value =  Gold
        self.assertEquals(4000, value)

if __name__ == '__main__':
    unittest.main()
Run Code Online (Sandbox Code Playgroud)

python unit-testing function assertions

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

在RSpec中捕获STDOUT

我是Ruby的单元测试的新手,我在创建一个检查STDOUT的规范时遇到了一些麻烦.我知道如果我正在测试的代码是在类/方法中,如何测试STDOUT,但我想测试puts来自不在Class或Method中的常规Ruby文件的语句.

这是一个简单的一个班轮 my_file.rb

puts "Welcome to the Book Club!"
Run Code Online (Sandbox Code Playgroud)

这是规格

my_spec.rb

require_relative 'my_file.rb'

  describe "Something I'm Testing" do
    it 'should print Welcome to the Book Club!' do

        expect {$stdout}.to output("Welcome to the Book Club!\n").to_stdout

      end
    end
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息:

Failures:

  1) Something I'm Testing should print Welcome to the Book Club!
     Failure/Error: expect {$stdout}.to output("Welcome to the Book Club!\n").to_stdout

       expected block to output "Welcome to the Book Club!\n" to stdout, but output nothing
       Diff:
       @@ -1,2 +1 @@
       -Welcome to …
Run Code Online (Sandbox Code Playgroud)

ruby unit-testing rspec

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

嵌套if语句的Python单元测试

所以我一直在发布单元测试问题,因为我正试图擅长他们.我会尽量保持清醒.下面我有一个嵌套的if语句,我想模拟difficulty变量的输入,如果条件满足则检查stdout.

这是我的运行代码..

def main():
    print("''''''''''''''''''''''''''''''''''''")
    print("''''''''''''''''''''''''''''''''''''")
    print("''' \t\t\t\t '''")
    print("''' \t\t\t\t '''")
    print("''' \t   Pirate Games\t\t '''")
    print("''' \tStart a new game?\t '''")
    print("''' \t\t\t\t '''")
    print("''' \t\t\t\t '''")
    print("''''''''''''''''''''''''''''''''''''")
    print("''''''''''''''''''''''''''''''''''''")

    newGame = input("").lower()

    if newGame == "yes" or "y":

        print("1.Scallywag\n2.Crew\n3.Pirate")
        difficulty = input("Choose ye toughness!")

        if difficulty == "1":
            print("TEST")

    elif newGame == "no" or "n":
        print("Goodbye! Come Again")

    else:
        print("Enter correct input!")
Run Code Online (Sandbox Code Playgroud)

现在这是我的单元测试,但这只适用于第一个If语句,它运行正常.我不知道如何去做第二个.感谢任何帮助,谢谢大家提前.

import unittest
from unittest.mock import patch
import io
import sys

from RunFile import …
Run Code Online (Sandbox Code Playgroud)

python unit-testing if-statement nested

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