# PySimpleTest
**Repository Path**: time-coder/PySimpleTest
## Basic Information
- **Project Name**: PySimpleTest
- **Description**: A very simple python test framework
- **Primary Language**: Python
- **License**: MIT
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2022-04-23
- **Last Updated**: 2023-10-14
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# PySimpleTest -- Make test as simple as possible
## 1 Why PySimpleTest
If you are suffering from writting just a simple for loop in **Robot Framework**, or if you are suffering from figuring out how fixture be called in **PyTest**, you come to the right place.
**PySimpleTest** use native python grammar and logic and make test very easy. It has following advantages:
* Use native python interpreter (not like pytest or robot framework).
* Only provide functions. No class, fixture, decorator or other weird things.
* Provide "super-realism" assertion system such as `should_become_true`, `should_keep_true`.
* Provide article liked log system. You can use `section`, `subsection`, ... to organize your test report.
* Provide many test assistant functions like `wait` with GUI progress bar, `say` to speake string out.
* Provide manual operation request functions like `please` and `please_check`
* Colored cmd output to indicate Fail, Pass, Error, etc.
* Log file with link information. If you use editor like Sublime, can realize double-click test report line to jump to corresponding code.
So for writting small test, PySimpleTest is a good choice. In addition, you can find PyPI index at: [https://pypi.org/project/PySimpleTest/](https://pypi.org/project/PySimpleTest/)
## 2 Getting Start
Write a file `main.py`:
```python
import PySimpleTest as pst
a = 2
pst.should_be_equal(a, 2)
pst.should_be_less(a, 1)
```
Then run it. You can get following cmd output:
And you can see 3 output file: `main.log`, `main.info` and `main.linfo`:
* `main.info` has the same content as console output.
* `main.linfo` has the same content as `main.info` but with `:` link information. It's for double-click jump purpose. See details in section [External Configuration](#sec_External_Configuration)
* `main.log`: `info` function will not save into `.log` file. See details in [info](#func_info) function description
So what functions can I use just like `should_be_equal` and `should_be_less`? See in section [Function List](#sec_Function_List)
## 3
This section will introduce all functions provided by `PySimpleTest`
### 3.1 Assertion System
For all assertion functions, will return `True` when Pass, return `False` when Fail. These function are listed below:
* `should_be_true(expression)`:
If `expression` is True, it will print "Pass: (<expression>) is True" and log in three output files.
Else "Fail: (<expression>) is False" will be printed and logged.
* `should_be_false(expression)`: Pass when `expression` is False.
* `should_be_equal(value1, value2)`: Pass when `value1 == value2`.
* `should_not_be_equal(value1, value2)`: Pass when `value1 != value2`.
* `should_be_less(value1, value2)`: Pass when `value1 < value2`.
* `should_not_be_less(value1, value2)`: Pass when `value1 >= value2`.
* `should_be_greater(value1, value2)`: Pass when `value1 > value2`.
* `should_not_be_greater(value1, value2)`: Pass when `value1 <= value2`.
* `should_be_approx(value1, value2, tolerance = 5, func = abs)`: Pass when `func(value1-value2) <= tolerance`.
* `should_not_be_approx(value1, value2, tolerance = 5, func = abs)`: Pass when `func(value1-value2) > tolerance`.
* `should_keep_true(expression, duration)`: Pass when `expression` keeps True for `duration` seconds.
Try following example:
```python
import PySimpleTest as pst
import time
start_time = time.time()
pst.should_keep_true(time.time()-start_time < 3, 2)
start_time = time.time()
pst.should_keep_true(time.time()-start_time < 3, 5)
```
* `should_keep_false(expression, duration)`: Pass when `expression` keeps False for `duration` seconds.
* `should_become_true(expression, timeout)`: Pass when `expression` becomes True in `timeout` seconds.
* `should_become_false(expression, timeout)`: Pass when `expression` becomes False in `timeout` seconds.
* `should_raise(expression, exception=None)`: Pass when `expression` raise a exception.
* If parameter `exception` is `None`, all exceptions raised will be passed;
* If parameter `exception` is an exception type such as `ZeroDivisionError`, all exceptions that are the instance of such type raised will be passed;
* If parameter `exception` is an exception instance such as `BaseException()`, only exception that just the same with such exception instance raised will be passed;
Try following example:
```python
import PySimpleTest as pst
pst.should_raise(lambda:1/0)
pst.should_raise(lambda:1/0, ZeroDivisionError)
pst.should_raise(lambda:1/0, NameError)
pst.should_raise(lambda:1/0, ZeroDivisionError("division by zero"))
```
* `should_not_raise(expression)`: Pass when `expression` dosen't raise any exception;
* `should_keep_raising(expression, duration, interval=0.1, exception=None)`: Pass when `expression` keeps raising `exception`;
* `should_keep_not_raising(expression, duration, interval=0.1)`: Pass when `expression` keeps raise nothing;
* `should_become_raising(expression, timeout=480, interval=0.1, exception=None)`: Pass when `expression` raise `exception` in `timeout` seconds;
* `should_become_not_raising(expression, timeout=480, interval=0.1)`: Pass when `expression` become not raising anything in `timeout` seconds;
> In above function list, all parameter named with `expression` can be a normal expression or a lambda expression. But you can only use lambda expression in Python console mode.
> Every function start with `should_` has it's blocked version start with `must_`. For example, `must_be_true(expression)`. `must_*` functions do the same thing as `should_*` functions only except when assertion is failed, `must_*` function will raise an `AssertionError` with fail message.
### 3.2 Logging System
* `log(*args, **kwargs)`:
Use `log` just like `print`. It will print in console as well as write into log file. For example:
```python
a = {"key": 5}
log("a =", a)
```
Default log file has the same base name with your python script but with expand name ".log" lays in the same folder with your python script. If you want to change path, use `--logfile` argument. See details in [Terminal Arguments](#sec_Terminal_Arguments).
In addition to `print`, `log` support color print, you can use `color` and `style` argument to control the print format. For example:
```
log("I am here!", color = "red", style = "highlight")
```
`color` can choose in list: `["black", "red", "green", "yellow", "blue", "purple", "cyan", "white"]`
`style` can choose in list: `["default", "highlight", "underline", "shining", "anti"]`
* :
Use `info` just like `log`. `info` will not write into `.log` file but into `.info` file. In fact, `log` will also write into `.info` file. Providing `info` function is aim to keep `.log` file clean. You can use `info` to print and note some assistant information. It will not disturb main test log file.
* `section(name, level = 1)`:
To make following log with one level indent. For example:
```python
section("Test eval function")
section("eval single value", level = 2)
should_be_equal(eval("1"), 1)
should_be_equal(eval("1.2"), 1.2)
should_be_equal(eval("-3.6"), -3.6)
should_be_equal(eval("True"), True)
section("eval math expression", level = 2)
should_be_equal(eval("3 + 5*2"), 13)
should_be_equal(eval("(6-2)*5"), 20)
```
Above code will have following output:
* `subsection(name)`: Same as `section(name, level = 2)`
* `subsubsection(name)`: Same as `section(name, level = 3)`
* `subsubsubsection(name)`: Same as `section(name, level = 4)`
* `subsubsubsubsection(name)`: Same as `section(name, level = 5)`
* `end_section()`: Will go back one level indent for following log. For example:
```python
log("line 1")
log("line 2")
section("section")
log("line 3")
log("line 4")
subsection("subsection")
log("line 5")
log("line 6")
end_section()
log("line 7")
log("line 8")
end_section()
log("line 9")
```
Above code will have following output. You can see that after `end_section()`, `line 7` and following log go back one level's indent, `line 9` and following log also go back one level's indent.
Also, you can use leveled section using class `Section`. For above example, you can rewrite in this way:
```python
log("line 1")
log("line 2")
with Section("section"):
log("line 3")
log("line 4")
with Section("subsection")
log("line 5")
log("line 6")
log("line 7")
log("line 8")
log("line 9")
```
It can also generate the same result as above figure. This is the recommanded way to use leveled section. Because result out log will have the same indent layout with the source code.
### 3.3 Header/Tailer information control
In a test report, following figure shows the header and tailer information position:
You can use following functions to control the output of Header/Tailer information:
* `title(name)`: to specify title in header information. If it is not used, title message in header information will use script base name.
* `author(name)`: to specify test author in header information. If it is not used, author will use your system user name.
* `version(name)`: Specify production version in header information.
* `url(link)`: Specify url in header information.
* `header_info[key] = value`: You can log more other "`: `" liked items in header information. For example:
```
header_info["Reviewer"] = "Eason"
```
* `tailer_info[key] = value`: In the same way, you can use `tailer_info` to log more "`: `" liked tailer information.
### 3.4 Test Assistant System
* `Pass(message)`: Same as `log("Pass:", message, color="green", style="highlight")`
* `Fail(message)`: Same as `log("Fail:", message, color="red", style="highlight")`
* `Skip(message)`: Same as `log("Skip:", message, color="green", style="highlight")`
* `wait(duration)`: Wait `duration` seconds. If `gui_on()` is called before and `duration` is greater than 10, The progress bar will pop out to indicate progress and time remain. Just like following figure:
* `wait_until(expression, timeout=480, interval=0.1, must=False)`: Wait until `` becomes True. If time waited more than `timeout`, it will stop waiting. `interval` indicate the time interval between two times `eval` of ``. If `must` is True, it will raise an `AssertionError` when timeout is reached.
* `wait_until_not(expression, timeout=480, interval=0.1, must=False)`: Similar with `wait_until`. Just to wait `` become False.
* `wait_until_raise(expression, exception=None, timeout=480, interval=0.1, must=False)`: Wait until `` raise `exception`.
* `wait_until_not_raise(expression, timeout=480, interval=0.1, must=False)`: Wait until `` not raise any exceptions.
* `please(do_something)`: If `gui_on()` is called before, it will pop up a message box to indicate you to do some manual operation. For example:
```python
please("reboot machine 1")
```
Then it will pop up following message box and wait you finish manual operation then click `OK` button.
If `gui_on()` is not called before, a console prompt will indicate you to enter after you finished manual operation.
* `please_check(something)`: If `gui_on()` is called before, it will pop up a message box to indicate you to do some manual check. This window will have two buttons: `Yes` and `No`:
* If you click `Yes`, it will log "Pass: (<somthing>) is True".
* If you click `No`, it will log "Fail: (<something>) is False".
If `gui_on()` is not called before, a console prompt will indicate you to input yes or no.
* `say(message)`: If `void_on()` is called before, you can use `say` to speak out message.
### 3.5 Configuration System
* `color_on()`: To turn on coloring console output. If your console out is just like following figure:
that means your console not support ASCII escape characters. Please use `color_off()` to turn off color. The default coloring print status is enabled.
* `color_off()`: To turn off coloring console output.
* `voice_on()`: To turn on voice. If voice is enable:
* a voice will say "Fail" when assertion failed;
* a voice will say out Exception Type when an internal exception is raised;
* a voice will say "Please <do something>" when `please` is called;
* a voice will say "Please check <something>" when `please_check` is called.
* `voice_off()`: To turn off voice. If voice is disable, nothing will speak out only except you use `say` function. The voice default status is disabled.
* `gui_on()`: To turn on gui. If gui is enable:
* a message box will pop up when `please` or `please_check` is called;
* a progressbar will pop up when `wait` or `should/must_keep_true/false` is called;
## 4
If you import `PySimpleTest`, you can use some terminal arguments to configure some thing. The terminal arguments formats is as following:
```
$ python