Code Monkey home page Code Monkey logo

python-bintest's Introduction

Bintest

Bintest is a extension for unitest, it will provide a way to run a command and assert the output.

Installation

pip install bintest

Usage

  1. Create a file in your project: testcase.py for example
from bintest import bintest
import unittest


class MyTestCase(unittest.TestCase, bintest.BinTest):
    __test__ = True

    def setUp(self):
        self.set_env(
            env="./output",  # temp dir
            input="./tests_case.yml",  # yml file
        )

    # This works
    def test_simple_cat(self):
        output, err = self.run_bin(name="SimpleCAT")
        # two ways of test, this both will do exactly the same
        self.assertEqual(output, "test\n")
        # this one is more auto because is reading from the yml
        self.assertOutput(name="SimpleCAT", output=output)

    # This Fails
    def test_simple_cat_fail(self):
        output, err = self.run_bin(name="SimpleCATFail")
        # this one will fail
        self.assertOutput(name="SimpleCATFail", output=output)


if __name__ == "__main__":
    unittest.main()
  1. Create the config file: tests_case.yml
---

# Example test file
# required: bin, output
# everything in the middle will be passing as paramters to the binary: only the values

SimpleCAT:
  bin: /bin/cat
  path: /Users/artur.gomes/projects/pybintest/examples/test.txt
  output: "test\n"

SimpleCATFail:
  bin: /bin/cat
  path: /Users/artur.gomes/projects/pybintest/examples/test.txt
  output: "not test\n"

In this case we are testing the cat command, using a file test.txt as argument and expecting the output

  1. Create the test.txt file
test
  1. Then run:
python3 -m unittest discover  -vvv
  1. Unittest will find yout test and run it for you.
test_simple_cat (testcase.MyTestCase) ... ok
test_simple_cat_fail (testcase.MyTestCase) ... FAIL

======================================================================
FAIL: test_simple_cat_fail (testcase.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/artur.gomes/projects/test_bintest/testcase.py", line 26, in test_simple_cat_fail
    self.assertOutput(name="SimpleCATFail", output=output)
  File "/Users/artur.gomes/Library/Caches/pypoetry/virtualenvs/test-bintest-LukI1uz2-py3.9/lib/python3.9/site-packages/bintest/bintest.py", line 34, in assertOutput
    raise self.failureException(msg)
AssertionError: Error: not test
 != test


----------------------------------------------------------------------
Ran 2 tests in 0.011s

FAILED (failures=1)

Contribution

If this helps you consider help me to improve.

python-bintest's People

Contributors

arturgoms avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.